home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / gd25s.zip / REGEX.C < prev    next >
C/C++ Source or Header  |  1993-10-08  |  174KB  |  5,156 lines

  1. /* Extended regular expression matching and search library,
  2.    version 0.12.
  3.    (Implements POSIX draft P10003.2/D11.2, except for
  4.    internationalization features.)
  5.  
  6.    Copyright (C) 1993 Free Software Foundation, Inc.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2, or (at your option)
  11.    any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. /* AIX requires this to be the first thing in the file. */
  23. #if defined (_AIX) && !defined (REGEX_MALLOC)
  24.   #pragma alloca
  25. #endif
  26.  
  27. #define _GNU_SOURCE
  28.  
  29. /* We need this for `regex.h', and perhaps for the Emacs include files.  */
  30. #include <sys/types.h>
  31.  
  32. #ifdef HAVE_CONFIG_H
  33. #include "config.h"
  34. #endif
  35.  
  36. /* The `emacs' switch turns on certain matching commands
  37.    that make sense only in Emacs. */
  38. #ifdef emacs
  39.  
  40. #include "lisp.h"
  41. #include "buffer.h"
  42. #include "syntax.h"
  43.  
  44. /* Emacs uses `NULL' as a predicate.  */
  45. #undef NULL
  46.  
  47. #else  /* not emacs */
  48.  
  49. #ifdef STDC_HEADERS
  50. #include <stdlib.h>
  51. #else
  52. char *malloc ();
  53. char *realloc ();
  54. #endif
  55.  
  56.  
  57. /* We used to test for `BSTRING' here, but only GCC and Emacs define
  58.    `BSTRING', as far as I know, and neither of them use this code.  */
  59. #if HAVE_STRING_H || STDC_HEADERS
  60. #include <string.h>
  61. #ifndef bcmp
  62. #define bcmp(s1, s2, n)    memcmp ((s1), (s2), (n))
  63. #endif
  64. #ifndef bcopy
  65. #define bcopy(s, d, n)    memcpy ((d), (s), (n))
  66. #endif
  67. #ifndef bzero
  68. #define bzero(s, n)    memset ((s), 0, (n))
  69. #endif
  70. #else
  71. #include <strings.h>
  72. #endif
  73.  
  74. /* Define the syntax stuff for \<, \>, etc.  */
  75.  
  76. /* This must be nonzero for the wordchar and notwordchar pattern
  77.    commands in re_match_2.  */
  78. #ifndef Sword 
  79. #define Sword 1
  80. #endif
  81.  
  82. #ifdef SYNTAX_TABLE
  83.  
  84. extern char *re_syntax_table;
  85.  
  86. #else /* not SYNTAX_TABLE */
  87.  
  88. /* How many characters in the character set.  */
  89. #define CHAR_SET_SIZE 256
  90.  
  91. static char re_syntax_table[CHAR_SET_SIZE];
  92.  
  93. static void
  94. init_syntax_once ()
  95. {
  96.    register int c;
  97.    static int done = 0;
  98.  
  99.    if (done)
  100.      return;
  101.  
  102.    bzero (re_syntax_table, sizeof re_syntax_table);
  103.  
  104.    for (c = 'a'; c <= 'z'; c++)
  105.      re_syntax_table[c] = Sword;
  106.  
  107.    for (c = 'A'; c <= 'Z'; c++)
  108.      re_syntax_table[c] = Sword;
  109.  
  110.    for (c = '0'; c <= '9'; c++)
  111.      re_syntax_table[c] = Sword;
  112.  
  113.    re_syntax_table['_'] = Sword;
  114.  
  115.    done = 1;
  116. }
  117.  
  118. #endif /* not SYNTAX_TABLE */
  119.  
  120. #define SYNTAX(c) re_syntax_table[c]
  121.  
  122. #endif /* not emacs */
  123.  
  124. /* Get the interface, including the syntax bits.  */
  125. #include "regex.h"
  126.  
  127. /* isalpha etc. are used for the character classes.  */
  128. #include <ctype.h>
  129.  
  130. /* Jim Meyering writes:
  131.  
  132.    "... Some ctype macros are valid only for character codes that
  133.    isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
  134.    using /bin/cc or gcc but without giving an ansi option).  So, all
  135.    ctype uses should be through macros like ISPRINT...  If
  136.    STDC_HEADERS is defined, then autoconf has verified that the ctype
  137.    macros don't need to be guarded with references to isascii. ...
  138.    Defining isascii to 1 should let any compiler worth its salt
  139.    eliminate the && through constant folding."  */
  140. #if ! defined (isascii) || defined (STDC_HEADERS)
  141. #undef isascii
  142. #define isascii(c) 1
  143. #endif
  144.  
  145. #ifdef isblank
  146. #define ISBLANK(c) (isascii (c) && isblank (c))
  147. #else
  148. #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  149. #endif
  150. #ifdef isgraph
  151. #define ISGRAPH(c) (isascii (c) && isgraph (c))
  152. #else
  153. #define ISGRAPH(c) (isascii (c) && isprint (c) && !isspace (c))
  154. #endif
  155.  
  156. #define ISPRINT(c) (isascii (c) && isprint (c))
  157. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  158. #define ISALNUM(c) (isascii (c) && isalnum (c))
  159. #define ISALPHA(c) (isascii (c) && isalpha (c))
  160. #define ISCNTRL(c) (isascii (c) && iscntrl (c))
  161. #define ISLOWER(c) (isascii (c) && islower (c))
  162. #define ISPUNCT(c) (isascii (c) && ispunct (c))
  163. #define ISSPACE(c) (isascii (c) && isspace (c))
  164. #define ISUPPER(c) (isascii (c) && isupper (c))
  165. #define ISXDIGIT(c) (isascii (c) && isxdigit (c))
  166.  
  167. #ifndef NULL
  168. #define NULL 0
  169. #endif
  170.  
  171. /* We remove any previous definition of `SIGN_EXTEND_CHAR',
  172.    since ours (we hope) works properly with all combinations of
  173.    machines, compilers, `char' and `unsigned char' argument types.
  174.    (Per Bothner suggested the basic approach.)  */
  175. #undef SIGN_EXTEND_CHAR
  176. #if __STDC__
  177. #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
  178. #else  /* not __STDC__ */
  179. /* As in Harbison and Steele.  */
  180. #define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
  181. #endif
  182.  
  183. /* Should we use malloc or alloca?  If REGEX_MALLOC is not defined, we
  184.    use `alloca' instead of `malloc'.  This is because using malloc in
  185.    re_search* or re_match* could cause memory leaks when C-g is used in
  186.    Emacs; also, malloc is slower and causes storage fragmentation.  On
  187.    the other hand, malloc is more portable, and easier to debug.  
  188.    
  189.    Because we sometimes use alloca, some routines have to be macros,
  190.    not functions -- `alloca'-allocated space disappears at the end of the
  191.    function it is called in.  */
  192.  
  193. #ifdef REGEX_MALLOC
  194.  
  195. #define REGEX_ALLOCATE malloc
  196. #define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
  197.  
  198. #else /* not REGEX_MALLOC  */
  199.  
  200. /* Emacs already defines alloca, sometimes.  */
  201. #ifndef alloca
  202.  
  203. /* Make alloca work the best possible way.  */
  204. #ifdef __GNUC__
  205. #define alloca __builtin_alloca
  206. #else /* not __GNUC__ */
  207. #if HAVE_ALLOCA_H
  208. #include <alloca.h>
  209. #else /* not __GNUC__ or HAVE_ALLOCA_H */
  210. #ifndef _AIX /* Already did AIX, up at the top.  */
  211. char *alloca ();
  212. #endif /* not _AIX */
  213. #endif /* not HAVE_ALLOCA_H */ 
  214. #endif /* not __GNUC__ */
  215.  
  216. #endif /* not alloca */
  217.  
  218. #define REGEX_ALLOCATE alloca
  219.  
  220. /* Assumes a `char *destination' variable.  */
  221. #define REGEX_REALLOCATE(source, osize, nsize)                \
  222.   (destination = (char *) alloca (nsize),                \
  223.    bcopy (source, destination, osize),                    \
  224.    destination)
  225.  
  226. #endif /* not REGEX_MALLOC */
  227.  
  228.  
  229. /* True if `size1' is non-NULL and PTR is pointing anywhere inside
  230.    `string1' or just past its end.  This works if PTR is NULL, which is
  231.    a good thing.  */
  232. #define FIRST_STRING_P(ptr)                     \
  233.   (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  234.  
  235. /* (Re)Allocate N items of type T using malloc, or fail.  */
  236. #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
  237. #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
  238. #define RETALLOC_IF(addr, n, t) \
  239.   if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
  240. #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
  241.  
  242. #define BYTEWIDTH 8 /* In bits.  */
  243.  
  244. #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
  245.  
  246. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  247. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  248.  
  249. typedef char boolean;
  250. #define false 0
  251. #define true 1
  252.  
  253. /* These are the command codes that appear in compiled regular
  254.    expressions.  Some opcodes are followed by argument bytes.  A
  255.    command code can specify any interpretation whatsoever for its
  256.    arguments.  Zero bytes may appear in the compiled regular expression.
  257.  
  258.    The value of `exactn' is needed in search.c (search_buffer) in Emacs.
  259.    So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
  260.    `exactn' we use here must also be 1.  */
  261.  
  262. typedef enum
  263. {
  264.   no_op = 0,
  265.  
  266.         /* Followed by one byte giving n, then by n literal bytes.  */
  267.   exactn = 1,
  268.  
  269.         /* Matches any (more or less) character.  */
  270.   anychar,
  271.  
  272.         /* Matches any one char belonging to specified set.  First
  273.            following byte is number of bitmap bytes.  Then come bytes
  274.            for a bitmap saying which chars are in.  Bits in each byte
  275.            are ordered low-bit-first.  A character is in the set if its
  276.            bit is 1.  A character too large to have a bit in the map is
  277.            automatically not in the set.  */
  278.   charset,
  279.  
  280.         /* Same parameters as charset, but match any character that is
  281.            not one of those specified.  */
  282.   charset_not,
  283.  
  284.         /* Start remembering the text that is matched, for storing in a
  285.            register.  Followed by one byte with the register number, in
  286.            the range 0 to one less than the pattern buffer's re_nsub
  287.            field.  Then followed by one byte with the number of groups
  288.            inner to this one.  (This last has to be part of the
  289.            start_memory only because we need it in the on_failure_jump
  290.            of re_match_2.)  */
  291.   start_memory,
  292.  
  293.         /* Stop remembering the text that is matched and store it in a
  294.            memory register.  Followed by one byte with the register
  295.            number, in the range 0 to one less than `re_nsub' in the
  296.            pattern buffer, and one byte with the number of inner groups,
  297.            just like `start_memory'.  (We need the number of inner
  298.            groups here because we don't have any easy way of finding the
  299.            corresponding start_memory when we're at a stop_memory.)  */
  300.   stop_memory,
  301.  
  302.         /* Match a duplicate of something remembered. Followed by one
  303.            byte containing the register number.  */
  304.   duplicate,
  305.  
  306.         /* Fail unless at beginning of line.  */
  307.   begline,
  308.  
  309.         /* Fail unless at end of line.  */
  310.   endline,
  311.  
  312.         /* Succeeds if at beginning of buffer (if emacs) or at beginning
  313.            of string to be matched (if not).  */
  314.   begbuf,
  315.  
  316.         /* Analogously, for end of buffer/string.  */
  317.   endbuf,
  318.  
  319.         /* Followed by two byte relative address to which to jump.  */
  320.   jump, 
  321.  
  322.     /* Same as jump, but marks the end of an alternative.  */
  323.   jump_past_alt,
  324.  
  325.         /* Followed by two-byte relative address of place to resume at
  326.            in case of failure.  */
  327.   on_failure_jump,
  328.     
  329.         /* Like on_failure_jump, but pushes a placeholder instead of the
  330.            current string position when executed.  */
  331.   on_failure_keep_string_jump,
  332.   
  333.         /* Throw away latest failure point and then jump to following
  334.            two-byte relative address.  */
  335.   pop_failure_jump,
  336.  
  337.         /* Change to pop_failure_jump if know won't have to backtrack to
  338.            match; otherwise change to jump.  This is used to jump
  339.            back to the beginning of a repeat.  If what follows this jump
  340.            clearly won't match what the repeat does, such that we can be
  341.            sure that there is no use backtracking out of repetitions
  342.            already matched, then we change it to a pop_failure_jump.
  343.            Followed by two-byte address.  */
  344.   maybe_pop_jump,
  345.  
  346.         /* Jump to following two-byte address, and push a dummy failure
  347.            point. This failure point will be thrown away if an attempt
  348.            is made to use it for a failure.  A `+' construct makes this
  349.            before the first repeat.  Also used as an intermediary kind
  350.            of jump when compiling an alternative.  */
  351.   dummy_failure_jump,
  352.  
  353.     /* Push a dummy failure point and continue.  Used at the end of
  354.        alternatives.  */
  355.   push_dummy_failure,
  356.  
  357.         /* Followed by two-byte relative address and two-byte number n.
  358.            After matching N times, jump to the address upon failure.  */
  359.   succeed_n,
  360.  
  361.         /* Followed by two-byte relative address, and two-byte number n.
  362.            Jump to the address N times, then fail.  */
  363.   jump_n,
  364.  
  365.         /* Set the following two-byte relative address to the
  366.            subsequent two-byte number.  The address *includes* the two
  367.            bytes of number.  */
  368.   set_number_at,
  369.  
  370.   wordchar,    /* Matches any word-constituent character.  */
  371.   notwordchar,    /* Matches any char that is not a word-constituent.  */
  372.  
  373.   wordbeg,    /* Succeeds if at word beginning.  */
  374.   wordend,    /* Succeeds if at word end.  */
  375.  
  376.   wordbound,    /* Succeeds if at a word boundary.  */
  377.   notwordbound    /* Succeeds if not at a word boundary.  */
  378.  
  379. #ifdef emacs
  380.   ,before_dot,    /* Succeeds if before point.  */
  381.   at_dot,    /* Succeeds if at point.  */
  382.   after_dot,    /* Succeeds if after point.  */
  383.  
  384.     /* Matches any character whose syntax is specified.  Followed by
  385.            a byte which contains a syntax code, e.g., Sword.  */
  386.   syntaxspec,
  387.  
  388.     /* Matches any character whose syntax is not that specified.  */
  389.   notsyntaxspec
  390. #endif /* emacs */
  391. } re_opcode_t;
  392.  
  393. /* Common operations on the compiled pattern.  */
  394.  
  395. /* Store NUMBER in two contiguous bytes starting at DESTINATION.  */
  396.  
  397. #define STORE_NUMBER(destination, number)                \
  398.   do {                                    \
  399.     (destination)[0] = (number) & 0377;                    \
  400.     (destination)[1] = (number) >> 8;                    \
  401.   } while (0)
  402.  
  403. /* Same as STORE_NUMBER, except increment DESTINATION to
  404.    the byte after where the number is stored.  Therefore, DESTINATION
  405.    must be an lvalue.  */
  406.  
  407. #define STORE_NUMBER_AND_INCR(destination, number)            \
  408.   do {                                    \
  409.     STORE_NUMBER (destination, number);                    \
  410.     (destination) += 2;                            \
  411.   } while (0)
  412.  
  413. /* Put into DESTINATION a number stored in two contiguous bytes starting
  414.    at SOURCE.  */
  415.  
  416. #define EXTRACT_NUMBER(destination, source)                \
  417.   do {                                    \
  418.     (destination) = *(source) & 0377;                    \
  419.     (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8;        \
  420.   } while (0)
  421.  
  422. #ifdef DEBUG
  423. static void
  424. extract_number (dest, source)
  425.     int *dest;
  426.     unsigned char *source;
  427. {
  428.   int temp = SIGN_EXTEND_CHAR (*(source + 1)); 
  429.   *dest = *source & 0377;
  430.   *dest += temp << 8;
  431. }
  432.  
  433. #ifndef EXTRACT_MACROS /* To debug the macros.  */
  434. #undef EXTRACT_NUMBER
  435. #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
  436. #endif /* not EXTRACT_MACROS */
  437.  
  438. #endif /* DEBUG */
  439.  
  440. /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
  441.    SOURCE must be an lvalue.  */
  442.  
  443. #define EXTRACT_NUMBER_AND_INCR(destination, source)            \
  444.   do {                                    \
  445.     EXTRACT_NUMBER (destination, source);                \
  446.     (source) += 2;                             \
  447.   } while (0)
  448.  
  449. #ifdef DEBUG
  450. static void
  451. extract_number_and_incr (destination, source)
  452.     int *destination;
  453.     unsigned char **source;
  454.   extract_number (destination, *source);
  455.   *source += 2;
  456. }
  457.  
  458. #ifndef EXTRACT_MACROS
  459. #undef EXTRACT_NUMBER_AND_INCR
  460. #define EXTRACT_NUMBER_AND_INCR(dest, src) \
  461.   extract_number_and_incr (&dest, &src)
  462. #endif /* not EXTRACT_MACROS */
  463.  
  464. #endif /* DEBUG */
  465.  
  466. /* If DEBUG is defined, Regex prints many voluminous messages about what
  467.    it is doing (if the variable `debug' is nonzero).  If linked with the
  468.    main program in `iregex.c', you can enter patterns and strings
  469.    interactively.  And if linked with the main program in `main.c' and
  470.    the other test files, you can run the already-written tests.  */
  471.  
  472. #ifdef DEBUG
  473.  
  474. /* We use standard I/O for debugging.  */
  475. #include <stdio.h>
  476.  
  477. /* It is useful to test things that ``must'' be true when debugging.  */
  478. #include <assert.h>
  479.  
  480. static int debug = 0;
  481.  
  482. #define DEBUG_STATEMENT(e) e
  483. #define DEBUG_PRINT1(x) if (debug) printf (x)
  484. #define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
  485. #define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
  486. #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
  487. #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)                 \
  488.   if (debug) print_partial_compiled_pattern (s, e)
  489. #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)            \
  490.   if (debug) print_double_string (w, s1, sz1, s2, sz2)
  491.  
  492.  
  493. extern void printchar ();
  494.  
  495. /* Print the fastmap in human-readable form.  */
  496.  
  497. void
  498. print_fastmap (fastmap)
  499.     char *fastmap;
  500. {
  501.   unsigned was_a_range = 0;
  502.   unsigned i = 0;  
  503.   
  504.   while (i < (1 << BYTEWIDTH))
  505.     {
  506.       if (fastmap[i++])
  507.     {
  508.       was_a_range = 0;
  509.           printchar (i - 1);
  510.           while (i < (1 << BYTEWIDTH)  &&  fastmap[i])
  511.             {
  512.               was_a_range = 1;
  513.               i++;
  514.             }
  515.       if (was_a_range)
  516.             {
  517.               printf ("-");
  518.               printchar (i - 1);
  519.             }
  520.         }
  521.     }
  522.   putchar ('\n'); 
  523. }
  524.  
  525.  
  526. /* Print a compiled pattern string in human-readable form, starting at
  527.    the START pointer into it and ending just before the pointer END.  */
  528.  
  529. void
  530. print_partial_compiled_pattern (start, end)
  531.     unsigned char *start;
  532.     unsigned char *end;
  533. {
  534.   int mcnt, mcnt2;
  535.   unsigned char *p = start;
  536.   unsigned char *pend = end;
  537.  
  538.   if (start == NULL)
  539.     {
  540.       printf ("(null)\n");
  541.       return;
  542.     }
  543.     
  544.   /* Loop over pattern commands.  */
  545.   while (p < pend)
  546.     {
  547.       printf ("%d:\t", p - start);
  548.  
  549.       switch ((re_opcode_t) *p++)
  550.     {
  551.         case no_op:
  552.           printf ("/no_op");
  553.           break;
  554.  
  555.     case exactn:
  556.       mcnt = *p++;
  557.           printf ("/exactn/%d", mcnt);
  558.           do
  559.         {
  560.               putchar ('/');
  561.           printchar (*p++);
  562.             }
  563.           while (--mcnt);
  564.           break;
  565.  
  566.     case start_memory:
  567.           mcnt = *p++;
  568.           printf ("/start_memory/%d/%d", mcnt, *p++);
  569.           break;
  570.  
  571.     case stop_memory:
  572.           mcnt = *p++;
  573.       printf ("/stop_memory/%d/%d", mcnt, *p++);
  574.           break;
  575.  
  576.     case duplicate:
  577.       printf ("/duplicate/%d", *p++);
  578.       break;
  579.  
  580.     case anychar:
  581.       printf ("/anychar");
  582.       break;
  583.  
  584.     case charset:
  585.         case charset_not:
  586.           {
  587.             register int c, last = -100;
  588.         register int in_range = 0;
  589.  
  590.         printf ("/charset [%s",
  591.                 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
  592.             
  593.             assert (p + *p < pend);
  594.  
  595.             for (c = 0; c < 256; c++)
  596.           if (c / 8 < *p
  597.           && (p[1 + (c/8)] & (1 << (c % 8))))
  598.         {
  599.           /* Are we starting a range?  */
  600.           if (last + 1 == c && ! in_range)
  601.             {
  602.               putchar ('-');
  603.               in_range = 1;
  604.             }
  605.           /* Have we broken a range?  */
  606.           else if (last + 1 != c && in_range)
  607.               {
  608.               printchar (last);
  609.               in_range = 0;
  610.             }
  611.                 
  612.           if (! in_range)
  613.             printchar (c);
  614.  
  615.           last = c;
  616.               }
  617.  
  618.         if (in_range)
  619.           printchar (last);
  620.  
  621.         putchar (']');
  622.  
  623.         p += 1 + *p;
  624.       }
  625.       break;
  626.  
  627.     case begline:
  628.       printf ("/begline");
  629.           break;
  630.  
  631.     case endline:
  632.           printf ("/endline");
  633.           break;
  634.  
  635.     case on_failure_jump:
  636.           extract_number_and_incr (&mcnt, &p);
  637.         printf ("/on_failure_jump to %d", p + mcnt - start);
  638.           break;
  639.  
  640.     case on_failure_keep_string_jump:
  641.           extract_number_and_incr (&mcnt, &p);
  642.         printf ("/on_failure_keep_string_jump to %d", p + mcnt - start);
  643.           break;
  644.  
  645.     case dummy_failure_jump:
  646.           extract_number_and_incr (&mcnt, &p);
  647.         printf ("/dummy_failure_jump to %d", p + mcnt - start);
  648.           break;
  649.  
  650.     case push_dummy_failure:
  651.           printf ("/push_dummy_failure");
  652.           break;
  653.           
  654.         case maybe_pop_jump:
  655.           extract_number_and_incr (&mcnt, &p);
  656.         printf ("/maybe_pop_jump to %d", p + mcnt - start);
  657.       break;
  658.  
  659.         case pop_failure_jump:
  660.       extract_number_and_incr (&mcnt, &p);
  661.         printf ("/pop_failure_jump to %d", p + mcnt - start);
  662.       break;          
  663.           
  664.         case jump_past_alt:
  665.       extract_number_and_incr (&mcnt, &p);
  666.         printf ("/jump_past_alt to %d", p + mcnt - start);
  667.       break;          
  668.           
  669.         case jump:
  670.       extract_number_and_incr (&mcnt, &p);
  671.         printf ("/jump to %d", p + mcnt - start);
  672.       break;
  673.  
  674.         case succeed_n: 
  675.           extract_number_and_incr (&mcnt, &p);
  676.           extract_number_and_incr (&mcnt2, &p);
  677.       printf ("/succeed_n to %d, %d times", p + mcnt - start, mcnt2);
  678.           break;
  679.         
  680.         case jump_n: 
  681.           extract_number_and_incr (&mcnt, &p);
  682.           extract_number_and_incr (&mcnt2, &p);
  683.       printf ("/jump_n to %d, %d times", p + mcnt - start, mcnt2);
  684.           break;
  685.         
  686.         case set_number_at: 
  687.           extract_number_and_incr (&mcnt, &p);
  688.           extract_number_and_incr (&mcnt2, &p);
  689.       printf ("/set_number_at location %d to %d", p + mcnt - start, mcnt2);
  690.           break;
  691.         
  692.         case wordbound:
  693.       printf ("/wordbound");
  694.       break;
  695.  
  696.     case notwordbound:
  697.       printf ("/notwordbound");
  698.           break;
  699.  
  700.     case wordbeg:
  701.       printf ("/wordbeg");
  702.       break;
  703.           
  704.     case wordend:
  705.       printf ("/wordend");
  706.           
  707. #ifdef emacs
  708.     case before_dot:
  709.       printf ("/before_dot");
  710.           break;
  711.  
  712.     case at_dot:
  713.       printf ("/at_dot");
  714.           break;
  715.  
  716.     case after_dot:
  717.       printf ("/after_dot");
  718.           break;
  719.  
  720.     case syntaxspec:
  721.           printf ("/syntaxspec");
  722.       mcnt = *p++;
  723.       printf ("/%d", mcnt);
  724.           break;
  725.       
  726.     case notsyntaxspec:
  727.           printf ("/notsyntaxspec");
  728.       mcnt = *p++;
  729.       printf ("/%d", mcnt);
  730.       break;
  731. #endif /* emacs */
  732.  
  733.     case wordchar:
  734.       printf ("/wordchar");
  735.           break;
  736.       
  737.     case notwordchar:
  738.       printf ("/notwordchar");
  739.           break;
  740.  
  741.     case begbuf:
  742.       printf ("/begbuf");
  743.           break;
  744.  
  745.     case endbuf:
  746.       printf ("/endbuf");
  747.           break;
  748.  
  749.         default:
  750.           printf ("?%d", *(p-1));
  751.     }
  752.  
  753.       putchar ('\n');
  754.     }
  755.  
  756.   printf ("%d:\tend of pattern.\n", p - start);
  757. }
  758.  
  759.  
  760. void
  761. print_compiled_pattern (bufp)
  762.     struct re_pattern_buffer *bufp;
  763. {
  764.   unsigned char *buffer = bufp->buffer;
  765.  
  766.   print_partial_compiled_pattern (buffer, buffer + bufp->used);
  767.   printf ("%d bytes used/%d bytes allocated.\n", bufp->used, bufp->allocated);
  768.  
  769.   if (bufp->fastmap_accurate && bufp->fastmap)
  770.     {
  771.       printf ("fastmap: ");
  772.       print_fastmap (bufp->fastmap);
  773.     }
  774.  
  775.   printf ("re_nsub: %d\t", bufp->re_nsub);
  776.   printf ("regs_alloc: %d\t", bufp->regs_allocated);
  777.   printf ("can_be_null: %d\t", bufp->can_be_null);
  778.   printf ("newline_anchor: %d\n", bufp->newline_anchor);
  779.   printf ("no_sub: %d\t", bufp->no_sub);
  780.   printf ("not_bol: %d\t", bufp->not_bol);
  781.   printf ("not_eol: %d\t", bufp->not_eol);
  782.   printf ("syntax: %d\n", bufp->syntax);
  783.   /* Perhaps we should print the translate table?  */
  784. }
  785.  
  786.  
  787. void
  788. print_double_string (where, string1, size1, string2, size2)
  789.     const char *where;
  790.     const char *string1;
  791.     const char *string2;
  792.     int size1;
  793.     int size2;
  794. {
  795.   unsigned this_char;
  796.   
  797.   if (where == NULL)
  798.     printf ("(null)");
  799.   else
  800.     {
  801.       if (FIRST_STRING_P (where))
  802.         {
  803.           for (this_char = where - string1; this_char < size1; this_char++)
  804.             printchar (string1[this_char]);
  805.  
  806.           where = string2;    
  807.         }
  808.  
  809.       for (this_char = where - string2; this_char < size2; this_char++)
  810.         printchar (string2[this_char]);
  811.     }
  812. }
  813.  
  814. #else /* not DEBUG */
  815.  
  816. #undef assert
  817. #define assert(e)
  818.  
  819. #define DEBUG_STATEMENT(e)
  820. #define DEBUG_PRINT1(x)
  821. #define DEBUG_PRINT2(x1, x2)
  822. #define DEBUG_PRINT3(x1, x2, x3)
  823. #define DEBUG_PRINT4(x1, x2, x3, x4)
  824. #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
  825. #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
  826.  
  827. #endif /* not DEBUG */
  828.  
  829. /* Set by `re_set_syntax' to the current regexp syntax to recognize.  Can
  830.    also be assigned to arbitrarily: each pattern buffer stores its own
  831.    syntax, so it can be changed between regex compilations.  */
  832. reg_syntax_t re_syntax_options = RE_SYNTAX_EMACS;
  833.  
  834.  
  835. /* Specify the precise syntax of regexps for compilation.  This provides
  836.    for compatibility for various utilities which historically have
  837.    different, incompatible syntaxes.
  838.  
  839.    The argument SYNTAX is a bit mask comprised of the various bits
  840.    defined in regex.h.  We return the old syntax.  */
  841.  
  842. reg_syntax_t
  843. re_set_syntax (syntax)
  844.     reg_syntax_t syntax;
  845. {
  846.   reg_syntax_t ret = re_syntax_options;
  847.   
  848.   re_syntax_options = syntax;
  849.   return ret;
  850. }
  851.  
  852. /* This table gives an error message for each of the error codes listed
  853.    in regex.h.  Obviously the order here has to be same as there.  */
  854.  
  855. static const char *re_error_msg[] =
  856.   { NULL,                    /* REG_NOERROR */
  857.     "No match",                    /* REG_NOMATCH */
  858.     "Invalid regular expression",        /* REG_BADPAT */
  859.     "Invalid collation character",        /* REG_ECOLLATE */
  860.     "Invalid character class name",        /* REG_ECTYPE */
  861.     "Trailing backslash",            /* REG_EESCAPE */
  862.     "Invalid back reference",            /* REG_ESUBREG */
  863.     "Unmatched [ or [^",            /* REG_EBRACK */
  864.     "Unmatched ( or \\(",            /* REG_EPAREN */
  865.     "Unmatched \\{",                /* REG_EBRACE */
  866.     "Invalid content of \\{\\}",        /* REG_BADBR */
  867.     "Invalid range end",            /* REG_ERANGE */
  868.     "Memory exhausted",                /* REG_ESPACE */
  869.     "Invalid preceding regular expression",    /* REG_BADRPT */
  870.     "Premature end of regular expression",    /* REG_EEND */
  871.     "Regular expression too big",        /* REG_ESIZE */
  872.     "Unmatched ) or \\)",            /* REG_ERPAREN */
  873.   };
  874.  
  875. /* Avoiding alloca during matching, to placate r_alloc.  */
  876.  
  877. /* Define MATCH_MAY_ALLOCATE if we need to make sure that the
  878.    searching and matching functions should not call alloca.  On some
  879.    systems, alloca is implemented in terms of malloc, and if we're
  880.    using the relocating allocator routines, then malloc could cause a
  881.    relocation, which might (if the strings being searched are in the
  882.    ralloc heap) shift the data out from underneath the regexp
  883.    routines.
  884.  
  885.    Here's another reason to avoid allocation: Emacs insists on
  886.    processing input from X in a signal handler; processing X input may
  887.    call malloc; if input arrives while a matching routine is calling
  888.    malloc, then we're scrod.  But Emacs can't just block input while
  889.    calling matching routines; then we don't notice interrupts when
  890.    they come in.  So, Emacs blocks input around all regexp calls
  891.    except the matching calls, which it leaves unprotected, in the
  892.    faith that they will not malloc.  */
  893.  
  894. /* Normally, this is fine.  */
  895. #define MATCH_MAY_ALLOCATE
  896.  
  897. /* But under some circumstances, it's not.  */
  898. #if defined (emacs) || (defined (REL_ALLOC) && defined (C_ALLOCA))
  899. #undef MATCH_MAY_ALLOCATE
  900. #endif
  901.  
  902.  
  903. /* Failure stack declarations and macros; both re_compile_fastmap and
  904.    re_match_2 use a failure stack.  These have to be macros because of
  905.    REGEX_ALLOCATE.  */
  906.    
  907.  
  908. /* Number of failure points for which to initially allocate space
  909.    when matching.  If this number is exceeded, we allocate more
  910.    space, so it is not a hard limit.  */
  911. #ifndef INIT_FAILURE_ALLOC
  912. #define INIT_FAILURE_ALLOC 5
  913. #endif
  914.  
  915. /* Roughly the maximum number of failure points on the stack.  Would be
  916.    exactly that if always used MAX_FAILURE_SPACE each time we failed.
  917.    This is a variable only so users of regex can assign to it; we never
  918.    change it ourselves.  */
  919. int re_max_failures = 2000;
  920.  
  921. typedef const unsigned char *fail_stack_elt_t;
  922.  
  923. typedef struct
  924. {
  925.   fail_stack_elt_t *stack;
  926.   unsigned size;
  927.   unsigned avail;            /* Offset of next open position.  */
  928. } fail_stack_type;
  929.  
  930. #define FAIL_STACK_EMPTY()     (fail_stack.avail == 0)
  931. #define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
  932. #define FAIL_STACK_FULL()      (fail_stack.avail == fail_stack.size)
  933. #define FAIL_STACK_TOP()       (fail_stack.stack[fail_stack.avail])
  934.  
  935.  
  936. /* Initialize `fail_stack'.  Do `return -2' if the alloc fails.  */
  937.  
  938. #ifdef MATCH_MAY_ALLOCATE
  939. #define INIT_FAIL_STACK()                        \
  940.   do {                                    \
  941.     fail_stack.stack = (fail_stack_elt_t *)                \
  942.       REGEX_ALLOCATE (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t));    \
  943.                                     \
  944.     if (fail_stack.stack == NULL)                    \
  945.       return -2;                            \
  946.                                     \
  947.     fail_stack.size = INIT_FAILURE_ALLOC;                \
  948.     fail_stack.avail = 0;                        \
  949.   } while (0)
  950. #else
  951. #define INIT_FAIL_STACK()                        \
  952.   do {                                    \
  953.     fail_stack.avail = 0;                        \
  954.   } while (0)
  955. #endif
  956.  
  957.  
  958. /* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
  959.  
  960.    Return 1 if succeeds, and 0 if either ran out of memory
  961.    allocating space for it or it was already too large.  
  962.    
  963.    REGEX_REALLOCATE requires `destination' be declared.   */
  964.  
  965. #define DOUBLE_FAIL_STACK(fail_stack)                    \
  966.   ((fail_stack).size > re_max_failures * MAX_FAILURE_ITEMS        \
  967.    ? 0                                    \
  968.    : ((fail_stack).stack = (fail_stack_elt_t *)                \
  969.         REGEX_REALLOCATE ((fail_stack).stack,                 \
  970.           (fail_stack).size * sizeof (fail_stack_elt_t),        \
  971.           ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)),    \
  972.                                     \
  973.       (fail_stack).stack == NULL                    \
  974.       ? 0                                \
  975.       : ((fail_stack).size <<= 1,                     \
  976.          1)))
  977.  
  978.  
  979. /* Push PATTERN_OP on FAIL_STACK. 
  980.  
  981.    Return 1 if was able to do so and 0 if ran out of memory allocating
  982.    space to do so.  */
  983. #define PUSH_PATTERN_OP(pattern_op, fail_stack)                \
  984.   ((FAIL_STACK_FULL ()                            \
  985.     && !DOUBLE_FAIL_STACK (fail_stack))                    \
  986.     ? 0                                    \
  987.     : ((fail_stack).stack[(fail_stack).avail++] = pattern_op,        \
  988.        1))
  989.  
  990. /* This pushes an item onto the failure stack.  Must be a four-byte
  991.    value.  Assumes the variable `fail_stack'.  Probably should only
  992.    be called from within `PUSH_FAILURE_POINT'.  */
  993. #define PUSH_FAILURE_ITEM(item)                        \
  994.   fail_stack.stack[fail_stack.avail++] = (fail_stack_elt_t) item
  995.  
  996. /* The complement operation.  Assumes `fail_stack' is nonempty.  */
  997. #define POP_FAILURE_ITEM() fail_stack.stack[--fail_stack.avail]
  998.  
  999. /* Used to omit pushing failure point id's when we're not debugging.  */
  1000. #ifdef DEBUG
  1001. #define DEBUG_PUSH PUSH_FAILURE_ITEM
  1002. #define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_ITEM ()
  1003. #else
  1004. #define DEBUG_PUSH(item)
  1005. #define DEBUG_POP(item_addr)
  1006. #endif
  1007.  
  1008.  
  1009. /* Push the information about the state we will need
  1010.    if we ever fail back to it.  
  1011.    
  1012.    Requires variables fail_stack, regstart, regend, reg_info, and
  1013.    num_regs be declared.  DOUBLE_FAIL_STACK requires `destination' be
  1014.    declared.
  1015.    
  1016.    Does `return FAILURE_CODE' if runs out of memory.  */
  1017.  
  1018. #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code)    \
  1019.   do {                                    \
  1020.     char *destination;                            \
  1021.     /* Must be int, so when we don't save any registers, the arithmetic    \
  1022.        of 0 + -1 isn't done as unsigned.  */                \
  1023.     int this_reg;                            \
  1024.                                         \
  1025.     DEBUG_STATEMENT (failure_id++);                    \
  1026.     DEBUG_STATEMENT (nfailure_points_pushed++);                \
  1027.     DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id);        \
  1028.     DEBUG_PRINT2 ("  Before push, next avail: %d\n", (fail_stack).avail);\
  1029.     DEBUG_PRINT2 ("                     size: %d\n", (fail_stack).size);\
  1030.                                     \
  1031.     DEBUG_PRINT2 ("  slots needed: %d\n", NUM_FAILURE_ITEMS);        \
  1032.     DEBUG_PRINT2 ("     available: %d\n", REMAINING_AVAIL_SLOTS);    \
  1033.                                     \
  1034.     /* Ensure we have enough space allocated for what we will push.  */    \
  1035.     while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS)            \
  1036.       {                                    \
  1037.         if (!DOUBLE_FAIL_STACK (fail_stack))            \
  1038.           return failure_code;                        \
  1039.                                     \
  1040.         DEBUG_PRINT2 ("\n  Doubled stack; size now: %d\n",        \
  1041.                (fail_stack).size);                \
  1042.         DEBUG_PRINT2 ("  slots available: %d\n", REMAINING_AVAIL_SLOTS);\
  1043.       }                                    \
  1044.                                     \
  1045.     /* Push the info, starting with the registers.  */            \
  1046.     DEBUG_PRINT1 ("\n");                        \
  1047.                                     \
  1048.     for (this_reg = lowest_active_reg; this_reg <= highest_active_reg;    \
  1049.          this_reg++)                            \
  1050.       {                                    \
  1051.     DEBUG_PRINT2 ("  Pushing reg: %d\n", this_reg);            \
  1052.         DEBUG_STATEMENT (num_regs_pushed++);                \
  1053.                                     \
  1054.     DEBUG_PRINT2 ("    start: 0x%x\n", regstart[this_reg]);        \
  1055.         PUSH_FAILURE_ITEM (regstart[this_reg]);                \
  1056.                                                                         \
  1057.     DEBUG_PRINT2 ("    end: 0x%x\n", regend[this_reg]);        \
  1058.         PUSH_FAILURE_ITEM (regend[this_reg]);                \
  1059.                                     \
  1060.     DEBUG_PRINT2 ("    info: 0x%x\n      ", reg_info[this_reg]);    \
  1061.         DEBUG_PRINT2 (" match_null=%d",                    \
  1062.                       REG_MATCH_NULL_STRING_P (reg_info[this_reg]));    \
  1063.         DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg]));    \
  1064.         DEBUG_PRINT2 (" matched_something=%d",                \
  1065.                       MATCHED_SOMETHING (reg_info[this_reg]));        \
  1066.         DEBUG_PRINT2 (" ever_matched=%d",                \
  1067.                       EVER_MATCHED_SOMETHING (reg_info[this_reg]));    \
  1068.     DEBUG_PRINT1 ("\n");                        \
  1069.         PUSH_FAILURE_ITEM (reg_info[this_reg].word);            \
  1070.       }                                    \
  1071.                                     \
  1072.     DEBUG_PRINT2 ("  Pushing  low active reg: %d\n", lowest_active_reg);\
  1073.     PUSH_FAILURE_ITEM (lowest_active_reg);                \
  1074.                                     \
  1075.     DEBUG_PRINT2 ("  Pushing high active reg: %d\n", highest_active_reg);\
  1076.     PUSH_FAILURE_ITEM (highest_active_reg);                \
  1077.                                     \
  1078.     DEBUG_PRINT2 ("  Pushing pattern 0x%x: ", pattern_place);        \
  1079.     DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend);        \
  1080.     PUSH_FAILURE_ITEM (pattern_place);                    \
  1081.                                     \
  1082.     DEBUG_PRINT2 ("  Pushing string 0x%x: `", string_place);        \
  1083.     DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2,   \
  1084.                  size2);                \
  1085.     DEBUG_PRINT1 ("'\n");                        \
  1086.     PUSH_FAILURE_ITEM (string_place);                    \
  1087.                                     \
  1088.     DEBUG_PRINT2 ("  Pushing failure id: %u\n", failure_id);        \
  1089.     DEBUG_PUSH (failure_id);                        \
  1090.   } while (0)
  1091.  
  1092. /* This is the number of items that are pushed and popped on the stack
  1093.    for each register.  */
  1094. #define NUM_REG_ITEMS  3
  1095.  
  1096. /* Individual items aside from the registers.  */
  1097. #ifdef DEBUG
  1098. #define NUM_NONREG_ITEMS 5 /* Includes failure point id.  */
  1099. #else
  1100. #define NUM_NONREG_ITEMS 4
  1101. #endif
  1102.  
  1103. /* We push at most this many items on the stack.  */
  1104. #define MAX_FAILURE_ITEMS ((num_regs - 1) * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
  1105.  
  1106. /* We actually push this many items.  */
  1107. #define NUM_FAILURE_ITEMS                        \
  1108.   ((highest_active_reg - lowest_active_reg + 1) * NUM_REG_ITEMS     \
  1109.     + NUM_NONREG_ITEMS)
  1110.  
  1111. /* How many items can still be added to the stack without overflowing it.  */
  1112. #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
  1113.  
  1114.  
  1115. /* Pops what PUSH_FAIL_STACK pushes.
  1116.  
  1117.    We restore into the parameters, all of which should be lvalues:
  1118.      STR -- the saved data position.
  1119.      PAT -- the saved pattern position.
  1120.      LOW_REG, HIGH_REG -- the highest and lowest active registers.
  1121.      REGSTART, REGEND -- arrays of string positions.
  1122.      REG_INFO -- array of information about each subexpression.
  1123.    
  1124.    Also assumes the variables `fail_stack' and (if debugging), `bufp',
  1125.    `pend', `string1', `size1', `string2', and `size2'.  */
  1126.  
  1127. #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
  1128. {                                    \
  1129.   DEBUG_STATEMENT (fail_stack_elt_t failure_id;)            \
  1130.   int this_reg;                                \
  1131.   const unsigned char *string_temp;                    \
  1132.                                     \
  1133.   assert (!FAIL_STACK_EMPTY ());                    \
  1134.                                     \
  1135.   /* Remove failure points and point to how many regs pushed.  */    \
  1136.   DEBUG_PRINT1 ("POP_FAILURE_POINT:\n");                \
  1137.   DEBUG_PRINT2 ("  Before pop, next avail: %d\n", fail_stack.avail);    \
  1138.   DEBUG_PRINT2 ("                    size: %d\n", fail_stack.size);    \
  1139.                                     \
  1140.   assert (fail_stack.avail >= NUM_NONREG_ITEMS);            \
  1141.                                     \
  1142.   DEBUG_POP (&failure_id);                        \
  1143.   DEBUG_PRINT2 ("  Popping failure id: %u\n", failure_id);        \
  1144.                                     \
  1145.   /* If the saved string location is NULL, it came from an        \
  1146.      on_failure_keep_string_jump opcode, and we want to throw away the    \
  1147.      saved NULL, thus retaining our current position in the string.  */    \
  1148.   string_temp = POP_FAILURE_ITEM ();                    \
  1149.   if (string_temp != NULL)                        \
  1150.     str = (const char *) string_temp;                    \
  1151.                                     \
  1152.   DEBUG_PRINT2 ("  Popping string 0x%x: `", str);            \
  1153.   DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2);    \
  1154.   DEBUG_PRINT1 ("'\n");                            \
  1155.                                     \
  1156.   pat = (unsigned char *) POP_FAILURE_ITEM ();                \
  1157.   DEBUG_PRINT2 ("  Popping pattern 0x%x: ", pat);            \
  1158.   DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend);            \
  1159.                                     \
  1160.   /* Restore register info.  */                        \
  1161.   high_reg = (unsigned) POP_FAILURE_ITEM ();                \
  1162.   DEBUG_PRINT2 ("  Popping high active reg: %d\n", high_reg);        \
  1163.                                     \
  1164.   low_reg = (unsigned) POP_FAILURE_ITEM ();                \
  1165.   DEBUG_PRINT2 ("  Popping  low active reg: %d\n", low_reg);        \
  1166.                                     \
  1167.   for (this_reg = high_reg; this_reg >= low_reg; this_reg--)        \
  1168.     {                                    \
  1169.       DEBUG_PRINT2 ("    Popping reg: %d\n", this_reg);            \
  1170.                                     \
  1171.       reg_info[this_reg].word = POP_FAILURE_ITEM ();            \
  1172.       DEBUG_PRINT2 ("      info: 0x%x\n", reg_info[this_reg]);        \
  1173.                                     \
  1174.       regend[this_reg] = (const char *) POP_FAILURE_ITEM ();        \
  1175.       DEBUG_PRINT2 ("      end: 0x%x\n", regend[this_reg]);        \
  1176.                                     \
  1177.       regstart[this_reg] = (const char *) POP_FAILURE_ITEM ();        \
  1178.       DEBUG_PRINT2 ("      start: 0x%x\n", regstart[this_reg]);        \
  1179.     }                                    \
  1180.                                     \
  1181.   DEBUG_STATEMENT (nfailure_points_popped++);                \
  1182. } /* POP_FAILURE_POINT */
  1183.  
  1184.  
  1185.  
  1186. /* Structure for per-register (a.k.a. per-group) information.
  1187.    This must not be longer than one word, because we push this value
  1188.    onto the failure stack.  Other register information, such as the
  1189.    starting and ending positions (which are addresses), and the list of
  1190.    inner groups (which is a bits list) are maintained in separate
  1191.    variables.  
  1192.    
  1193.    We are making a (strictly speaking) nonportable assumption here: that
  1194.    the compiler will pack our bit fields into something that fits into
  1195.    the type of `word', i.e., is something that fits into one item on the
  1196.    failure stack.  */
  1197. typedef union
  1198. {
  1199.   fail_stack_elt_t word;
  1200.   struct
  1201.   {
  1202.       /* This field is one if this group can match the empty string,
  1203.          zero if not.  If not yet determined,  `MATCH_NULL_UNSET_VALUE'.  */
  1204. #define MATCH_NULL_UNSET_VALUE 3
  1205.     unsigned match_null_string_p : 2;
  1206.     unsigned is_active : 1;
  1207.     unsigned matched_something : 1;
  1208.     unsigned ever_matched_something : 1;
  1209.   } bits;
  1210. } register_info_type;
  1211.  
  1212. #define REG_MATCH_NULL_STRING_P(R)  ((R).bits.match_null_string_p)
  1213. #define IS_ACTIVE(R)  ((R).bits.is_active)
  1214. #define MATCHED_SOMETHING(R)  ((R).bits.matched_something)
  1215. #define EVER_MATCHED_SOMETHING(R)  ((R).bits.ever_matched_something)
  1216.  
  1217.  
  1218. /* Call this when have matched a real character; it sets `matched' flags
  1219.    for the subexpressions which we are currently inside.  Also records
  1220.    that those subexprs have matched.  */
  1221. #define SET_REGS_MATCHED()                        \
  1222.   do                                    \
  1223.     {                                    \
  1224.       unsigned r;                            \
  1225.       for (r = lowest_active_reg; r <= highest_active_reg; r++)        \
  1226.         {                                \
  1227.           MATCHED_SOMETHING (reg_info[r])                \
  1228.             = EVER_MATCHED_SOMETHING (reg_info[r])            \
  1229.             = 1;                            \
  1230.         }                                \
  1231.     }                                    \
  1232.   while (0)
  1233.  
  1234.  
  1235. /* Registers are set to a sentinel when they haven't yet matched.  */
  1236. #define REG_UNSET_VALUE ((char *) -1)
  1237. #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
  1238.  
  1239.  
  1240.  
  1241. /* How do we implement a missing MATCH_MAY_ALLOCATE?
  1242.    We make the fail stack a global thing, and then grow it to
  1243.    re_max_failures when we compile.  */
  1244. #ifndef MATCH_MAY_ALLOCATE
  1245. static fail_stack_type fail_stack;
  1246.  
  1247. static const char **     regstart, **     regend;
  1248. static const char ** old_regstart, ** old_regend;
  1249. static const char **best_regstart, **best_regend;
  1250. static register_info_type *reg_info; 
  1251. static const char **reg_dummy;
  1252. static register_info_type *reg_info_dummy;
  1253. #endif
  1254.  
  1255.  
  1256. /* Subroutine declarations and macros for regex_compile.  */
  1257.  
  1258. static void store_op1 (), store_op2 ();
  1259. static void insert_op1 (), insert_op2 ();
  1260. static boolean at_begline_loc_p (), at_endline_loc_p ();
  1261. static boolean group_in_compile_stack ();
  1262. static reg_errcode_t compile_range ();
  1263.  
  1264. /* Fetch the next character in the uncompiled pattern---translating it 
  1265.    if necessary.  Also cast from a signed character in the constant
  1266.    string passed to us by the user to an unsigned char that we can use
  1267.    as an array index (in, e.g., `translate').  */
  1268. #define PATFETCH(c)                            \
  1269.   do {if (p == pend) return REG_EEND;                    \
  1270.     c = (unsigned char) *p++;                        \
  1271.     if (translate) c = translate[c];                     \
  1272.   } while (0)
  1273.  
  1274. /* Fetch the next character in the uncompiled pattern, with no
  1275.    translation.  */
  1276. #define PATFETCH_RAW(c)                            \
  1277.   do {if (p == pend) return REG_EEND;                    \
  1278.     c = (unsigned char) *p++;                         \
  1279.   } while (0)
  1280.  
  1281. /* Go backwards one character in the pattern.  */
  1282. #define PATUNFETCH p--
  1283.  
  1284.  
  1285. /* If `translate' is non-null, return translate[D], else just D.  We
  1286.    cast the subscript to translate because some data is declared as
  1287.    `char *', to avoid warnings when a string constant is passed.  But
  1288.    when we use a character as a subscript we must make it unsigned.  */
  1289. #define TRANSLATE(d) (translate ? translate[(unsigned char) (d)] : (d))
  1290.  
  1291.  
  1292. /* Macros for outputting the compiled pattern into `buffer'.  */
  1293.  
  1294. /* If the buffer isn't allocated when it comes in, use this.  */
  1295. #define INIT_BUF_SIZE  32
  1296.  
  1297. /* Make sure we have at least N more bytes of space in buffer.  */
  1298. #define GET_BUFFER_SPACE(n)                        \
  1299.     while (b - bufp->buffer + (n) > bufp->allocated)            \
  1300.       EXTEND_BUFFER ()
  1301.  
  1302. /* Make sure we have one more byte of buffer space and then add C to it.  */
  1303. #define BUF_PUSH(c)                            \
  1304.   do {                                    \
  1305.     GET_BUFFER_SPACE (1);                        \
  1306.     *b++ = (unsigned char) (c);                        \
  1307.   } while (0)
  1308.  
  1309.  
  1310. /* Ensure we have two more bytes of buffer space and then append C1 and C2.  */
  1311. #define BUF_PUSH_2(c1, c2)                        \
  1312.   do {                                    \
  1313.     GET_BUFFER_SPACE (2);                        \
  1314.     *b++ = (unsigned char) (c1);                    \
  1315.     *b++ = (unsigned char) (c2);                    \
  1316.   } while (0)
  1317.  
  1318.  
  1319. /* As with BUF_PUSH_2, except for three bytes.  */
  1320. #define BUF_PUSH_3(c1, c2, c3)                        \
  1321.   do {                                    \
  1322.     GET_BUFFER_SPACE (3);                        \
  1323.     *b++ = (unsigned char) (c1);                    \
  1324.     *b++ = (unsigned char) (c2);                    \
  1325.     *b++ = (unsigned char) (c3);                    \
  1326.   } while (0)
  1327.  
  1328.  
  1329. /* Store a jump with opcode OP at LOC to location TO.  We store a
  1330.    relative address offset by the three bytes the jump itself occupies.  */
  1331. #define STORE_JUMP(op, loc, to) \
  1332.   store_op1 (op, loc, (to) - (loc) - 3)
  1333.  
  1334. /* Likewise, for a two-argument jump.  */
  1335. #define STORE_JUMP2(op, loc, to, arg) \
  1336.   store_op2 (op, loc, (to) - (loc) - 3, arg)
  1337.  
  1338. /* Like `STORE_JUMP', but for inserting.  Assume `b' is the buffer end.  */
  1339. #define INSERT_JUMP(op, loc, to) \
  1340.   insert_op1 (op, loc, (to) - (loc) - 3, b)
  1341.  
  1342. /* Like `STORE_JUMP2', but for inserting.  Assume `b' is the buffer end.  */
  1343. #define INSERT_JUMP2(op, loc, to, arg) \
  1344.   insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
  1345.  
  1346.  
  1347. /* This is not an arbitrary limit: the arguments which represent offsets
  1348.    into the pattern are two bytes long.  So if 2^16 bytes turns out to
  1349.    be too small, many things would have to change.  */
  1350. #define MAX_BUF_SIZE (1L << 16)
  1351.  
  1352.  
  1353. /* Extend the buffer by twice its current size via realloc and
  1354.    reset the pointers that pointed into the old block to point to the
  1355.    correct places in the new one.  If extending the buffer results in it
  1356.    being larger than MAX_BUF_SIZE, then flag memory exhausted.  */
  1357. #define EXTEND_BUFFER()                            \
  1358.   do {                                     \
  1359.     unsigned char *old_buffer = bufp->buffer;                \
  1360.     if (bufp->allocated == MAX_BUF_SIZE)                 \
  1361.       return REG_ESIZE;                            \
  1362.     bufp->allocated <<= 1;                        \
  1363.     if (bufp->allocated > MAX_BUF_SIZE)                    \
  1364.       bufp->allocated = MAX_BUF_SIZE;                     \
  1365.     bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
  1366.     if (bufp->buffer == NULL)                        \
  1367.       return REG_ESPACE;                        \
  1368.     /* If the buffer moved, move all the pointers into it.  */        \
  1369.     if (old_buffer != bufp->buffer)                    \
  1370.       {                                    \
  1371.         b = (b - old_buffer) + bufp->buffer;                \
  1372.         begalt = (begalt - old_buffer) + bufp->buffer;            \
  1373.         if (fixup_alt_jump)                        \
  1374.           fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
  1375.         if (laststart)                            \
  1376.           laststart = (laststart - old_buffer) + bufp->buffer;        \
  1377.         if (pending_exact)                        \
  1378.           pending_exact = (pending_exact - old_buffer) + bufp->buffer;    \
  1379.       }                                    \
  1380.   } while (0)
  1381.  
  1382.  
  1383. /* Since we have one byte reserved for the register number argument to
  1384.    {start,stop}_memory, the maximum number of groups we can report
  1385.    things about is what fits in that byte.  */
  1386. #define MAX_REGNUM 255
  1387.  
  1388. /* But patterns can have more than `MAX_REGNUM' registers.  We just
  1389.    ignore the excess.  */
  1390. typedef unsigned regnum_t;
  1391.  
  1392.  
  1393. /* Macros for the compile stack.  */
  1394.  
  1395. /* Since offsets can go either forwards or backwards, this type needs to
  1396.    be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1.  */
  1397. typedef int pattern_offset_t;
  1398.  
  1399. typedef struct
  1400. {
  1401.   pattern_offset_t begalt_offset;
  1402.   pattern_offset_t fixup_alt_jump;
  1403.   pattern_offset_t inner_group_offset;
  1404.   pattern_offset_t laststart_offset;  
  1405.   regnum_t regnum;
  1406. } compile_stack_elt_t;
  1407.  
  1408.  
  1409. typedef struct
  1410. {
  1411.   compile_stack_elt_t *stack;
  1412.   unsigned size;
  1413.   unsigned avail;            /* Offset of next open position.  */
  1414. } compile_stack_type;
  1415.  
  1416.  
  1417. #define INIT_COMPILE_STACK_SIZE 32
  1418.  
  1419. #define COMPILE_STACK_EMPTY  (compile_stack.avail == 0)
  1420. #define COMPILE_STACK_FULL  (compile_stack.avail == compile_stack.size)
  1421.  
  1422. /* The next available element.  */
  1423. #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
  1424.  
  1425.  
  1426. /* Set the bit for character C in a list.  */
  1427. #define SET_LIST_BIT(c)                               \
  1428.   (b[((unsigned char) (c)) / BYTEWIDTH]               \
  1429.    |= 1 << (((unsigned char) c) % BYTEWIDTH))
  1430.  
  1431.  
  1432. /* Get the next unsigned number in the uncompiled pattern.  */
  1433. #define GET_UNSIGNED_NUMBER(num)                     \
  1434.   { if (p != pend)                            \
  1435.      {                                    \
  1436.        PATFETCH (c);                             \
  1437.        while (ISDIGIT (c))                         \
  1438.          {                                 \
  1439.            if (num < 0)                            \
  1440.               num = 0;                            \
  1441.            num = num * 10 + c - '0';                     \
  1442.            if (p == pend)                         \
  1443.               break;                             \
  1444.            PATFETCH (c);                        \
  1445.          }                                 \
  1446.        }                                 \
  1447.     }        
  1448.  
  1449. #define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
  1450.  
  1451. #define IS_CHAR_CLASS(string)                        \
  1452.    (STREQ (string, "alpha") || STREQ (string, "upper")            \
  1453.     || STREQ (string, "lower") || STREQ (string, "digit")        \
  1454.     || STREQ (string, "alnum") || STREQ (string, "xdigit")        \
  1455.     || STREQ (string, "space") || STREQ (string, "print")        \
  1456.     || STREQ (string, "punct") || STREQ (string, "graph")        \
  1457.     || STREQ (string, "cntrl") || STREQ (string, "blank"))
  1458.  
  1459. /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
  1460.    Returns one of error codes defined in `regex.h', or zero for success.
  1461.  
  1462.    Assumes the `allocated' (and perhaps `buffer') and `translate'
  1463.    fields are set in BUFP on entry.
  1464.  
  1465.    If it succeeds, results are put in BUFP (if it returns an error, the
  1466.    contents of BUFP are undefined):
  1467.      `buffer' is the compiled pattern;
  1468.      `syntax' is set to SYNTAX;
  1469.      `used' is set to the length of the compiled pattern;
  1470.      `fastmap_accurate' is zero;
  1471.      `re_nsub' is the number of subexpressions in PATTERN;
  1472.      `not_bol' and `not_eol' are zero;
  1473.    
  1474.    The `fastmap' and `newline_anchor' fields are neither
  1475.    examined nor set.  */
  1476.  
  1477. static reg_errcode_t
  1478. regex_compile (pattern, size, syntax, bufp)
  1479.      const char *pattern;
  1480.      int size;
  1481.      reg_syntax_t syntax;
  1482.      struct re_pattern_buffer *bufp;
  1483. {
  1484.   /* We fetch characters from PATTERN here.  Even though PATTERN is
  1485.      `char *' (i.e., signed), we declare these variables as unsigned, so
  1486.      they can be reliably used as array indices.  */
  1487.   register unsigned char c, c1;
  1488.   
  1489.   /* A random tempory spot in PATTERN.  */
  1490.   const char *p1;
  1491.  
  1492.   /* Points to the end of the buffer, where we should append.  */
  1493.   register unsigned char *b;
  1494.   
  1495.   /* Keeps track of unclosed groups.  */
  1496.   compile_stack_type compile_stack;
  1497.  
  1498.   /* Points to the current (ending) position in the pattern.  */
  1499.   const char *p = pattern;
  1500.   const char *pend = pattern + size;
  1501.   
  1502.   /* How to translate the characters in the pattern.  */
  1503.   char *translate = bufp->translate;
  1504.  
  1505.   /* Address of the count-byte of the most recently inserted `exactn'
  1506.      command.  This makes it possible to tell if a new exact-match
  1507.      character can be added to that command or if the character requires
  1508.      a new `exactn' command.  */
  1509.   unsigned char *pending_exact = 0;
  1510.  
  1511.   /* Address of start of the most recently finished expression.
  1512.      This tells, e.g., postfix * where to find the start of its
  1513.      operand.  Reset at the beginning of groups and alternatives.  */
  1514.   unsigned char *laststart = 0;
  1515.  
  1516.   /* Address of beginning of regexp, or inside of last group.  */
  1517.   unsigned char *begalt;
  1518.  
  1519.   /* Place in the uncompiled pattern (i.e., the {) to
  1520.      which to go back if the interval is invalid.  */
  1521.   const char *beg_interval;
  1522.                 
  1523.   /* Address of the place where a forward jump should go to the end of
  1524.      the containing expression.  Each alternative of an `or' -- except the
  1525.      last -- ends with a forward jump of this sort.  */
  1526.   unsigned char *fixup_alt_jump = 0;
  1527.  
  1528.   /* Counts open-groups as they are encountered.  Remembered for the
  1529.      matching close-group on the compile stack, so the same register
  1530.      number is put in the stop_memory as the start_memory.  */
  1531.   regnum_t regnum = 0;
  1532.  
  1533. #ifdef DEBUG
  1534.   DEBUG_PRINT1 ("\nCompiling pattern: ");
  1535.   if (debug)
  1536.     {
  1537.       unsigned debug_count;
  1538.       
  1539.       for (debug_count = 0; debug_count < size; debug_count++)
  1540.         printchar (pattern[debug_count]);
  1541.       putchar ('\n');
  1542.     }
  1543. #endif /* DEBUG */
  1544.  
  1545.   /* Initialize the compile stack.  */
  1546.   compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
  1547.   if (compile_stack.stack == NULL)
  1548.     return REG_ESPACE;
  1549.  
  1550.   compile_stack.size = INIT_COMPILE_STACK_SIZE;
  1551.   compile_stack.avail = 0;
  1552.  
  1553.   /* Initialize the pattern buffer.  */
  1554.   bufp->syntax = syntax;
  1555.   bufp->fastmap_accurate = 0;
  1556.   bufp->not_bol = bufp->not_eol = 0;
  1557.  
  1558.   /* Set `used' to zero, so that if we return an error, the pattern
  1559.      printer (for debugging) will think there's no pattern.  We reset it
  1560.      at the end.  */
  1561.   bufp->used = 0;
  1562.   
  1563.   /* Always count groups, whether or not bufp->no_sub is set.  */
  1564.   bufp->re_nsub = 0;                
  1565.  
  1566. #if !defined (emacs) && !defined (SYNTAX_TABLE)
  1567.   /* Initialize the syntax table.  */
  1568.    init_syntax_once ();
  1569. #endif
  1570.  
  1571.   if (bufp->allocated == 0)
  1572.     {
  1573.       if (bufp->buffer)
  1574.     { /* If zero allocated, but buffer is non-null, try to realloc
  1575.              enough space.  This loses if buffer's address is bogus, but
  1576.              that is the user's responsibility.  */
  1577.           RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
  1578.         }
  1579.       else
  1580.         { /* Caller did not allocate a buffer.  Do it for them.  */
  1581.           bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
  1582.         }
  1583.       if (!bufp->buffer) return REG_ESPACE;
  1584.  
  1585.       bufp->allocated = INIT_BUF_SIZE;
  1586.     }
  1587.  
  1588.   begalt = b = bufp->buffer;
  1589.  
  1590.   /* Loop through the uncompiled pattern until we're at the end.  */
  1591.   while (p != pend)
  1592.     {
  1593.       PATFETCH (c);
  1594.  
  1595.       switch (c)
  1596.         {
  1597.         case '^':
  1598.           {
  1599.             if (   /* If at start of pattern, it's an operator.  */
  1600.                    p == pattern + 1
  1601.                    /* If context independent, it's an operator.  */
  1602.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  1603.                    /* Otherwise, depends on what's come before.  */
  1604.                 || at_begline_loc_p (pattern, p, syntax))
  1605.               BUF_PUSH (begline);
  1606.             else
  1607.               goto normal_char;
  1608.           }
  1609.           break;
  1610.  
  1611.  
  1612.         case '$':
  1613.           {
  1614.             if (   /* If at end of pattern, it's an operator.  */
  1615.                    p == pend 
  1616.                    /* If context independent, it's an operator.  */
  1617.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  1618.                    /* Otherwise, depends on what's next.  */
  1619.                 || at_endline_loc_p (p, pend, syntax))
  1620.                BUF_PUSH (endline);
  1621.              else
  1622.                goto normal_char;
  1623.            }
  1624.            break;
  1625.  
  1626.  
  1627.     case '+':
  1628.         case '?':
  1629.           if ((syntax & RE_BK_PLUS_QM)
  1630.               || (syntax & RE_LIMITED_OPS))
  1631.             goto normal_char;
  1632.         handle_plus:
  1633.         case '*':
  1634.           /* If there is no previous pattern... */
  1635.           if (!laststart)
  1636.             {
  1637.               if (syntax & RE_CONTEXT_INVALID_OPS)
  1638.                 return REG_BADRPT;
  1639.               else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  1640.                 goto normal_char;
  1641.             }
  1642.  
  1643.           {
  1644.             /* Are we optimizing this jump?  */
  1645.             boolean keep_string_p = false;
  1646.             
  1647.             /* 1 means zero (many) matches is allowed.  */
  1648.             char zero_times_ok = 0, many_times_ok = 0;
  1649.  
  1650.             /* If there is a sequence of repetition chars, collapse it
  1651.                down to just one (the right one).  We can't combine
  1652.                interval operators with these because of, e.g., `a{2}*',
  1653.                which should only match an even number of `a's.  */
  1654.  
  1655.             for (;;)
  1656.               {
  1657.                 zero_times_ok |= c != '+';
  1658.                 many_times_ok |= c != '?';
  1659.  
  1660.                 if (p == pend)
  1661.                   break;
  1662.  
  1663.                 PATFETCH (c);
  1664.  
  1665.                 if (c == '*'
  1666.                     || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
  1667.                   ;
  1668.  
  1669.                 else if (syntax & RE_BK_PLUS_QM  &&  c == '\\')
  1670.                   {
  1671.                     if (p == pend) return REG_EESCAPE;
  1672.  
  1673.                     PATFETCH (c1);
  1674.                     if (!(c1 == '+' || c1 == '?'))
  1675.                       {
  1676.                         PATUNFETCH;
  1677.                         PATUNFETCH;
  1678.                         break;
  1679.                       }
  1680.  
  1681.                     c = c1;
  1682.                   }
  1683.                 else
  1684.                   {
  1685.                     PATUNFETCH;
  1686.                     break;
  1687.                   }
  1688.  
  1689.                 /* If we get here, we found another repeat character.  */
  1690.                }
  1691.  
  1692.             /* Star, etc. applied to an empty pattern is equivalent
  1693.                to an empty pattern.  */
  1694.             if (!laststart)  
  1695.               break;
  1696.  
  1697.             /* Now we know whether or not zero matches is allowed
  1698.                and also whether or not two or more matches is allowed.  */
  1699.             if (many_times_ok)
  1700.               { /* More than one repetition is allowed, so put in at the
  1701.                    end a backward relative jump from `b' to before the next
  1702.                    jump we're going to put in below (which jumps from
  1703.                    laststart to after this jump).  
  1704.  
  1705.                    But if we are at the `*' in the exact sequence `.*\n',
  1706.                    insert an unconditional jump backwards to the .,
  1707.                    instead of the beginning of the loop.  This way we only
  1708.                    push a failure point once, instead of every time
  1709.                    through the loop.  */
  1710.                 assert (p - 1 > pattern);
  1711.  
  1712.                 /* Allocate the space for the jump.  */
  1713.                 GET_BUFFER_SPACE (3);
  1714.  
  1715.                 /* We know we are not at the first character of the pattern,
  1716.                    because laststart was nonzero.  And we've already
  1717.                    incremented `p', by the way, to be the character after
  1718.                    the `*'.  Do we have to do something analogous here
  1719.                    for null bytes, because of RE_DOT_NOT_NULL?  */
  1720.                 if (TRANSLATE (*(p - 2)) == TRANSLATE ('.')
  1721.             && zero_times_ok
  1722.                     && p < pend && TRANSLATE (*p) == TRANSLATE ('\n')
  1723.                     && !(syntax & RE_DOT_NEWLINE))
  1724.                   { /* We have .*\n.  */
  1725.                     STORE_JUMP (jump, b, laststart);
  1726.                     keep_string_p = true;
  1727.                   }
  1728.                 else
  1729.                   /* Anything else.  */
  1730.                   STORE_JUMP (maybe_pop_jump, b, laststart - 3);
  1731.  
  1732.                 /* We've added more stuff to the buffer.  */
  1733.                 b += 3;
  1734.               }
  1735.  
  1736.             /* On failure, jump from laststart to b + 3, which will be the
  1737.                end of the buffer after this jump is inserted.  */
  1738.             GET_BUFFER_SPACE (3);
  1739.             INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
  1740.                                        : on_failure_jump,
  1741.                          laststart, b + 3);
  1742.             pending_exact = 0;
  1743.             b += 3;
  1744.  
  1745.             if (!zero_times_ok)
  1746.               {
  1747.                 /* At least one repetition is required, so insert a
  1748.                    `dummy_failure_jump' before the initial
  1749.                    `on_failure_jump' instruction of the loop. This
  1750.                    effects a skip over that instruction the first time
  1751.                    we hit that loop.  */
  1752.                 GET_BUFFER_SPACE (3);
  1753.                 INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6);
  1754.                 b += 3;
  1755.               }
  1756.             }
  1757.       break;
  1758.  
  1759.  
  1760.     case '.':
  1761.           laststart = b;
  1762.           BUF_PUSH (anychar);
  1763.           break;
  1764.  
  1765.  
  1766.         case '[':
  1767.           {
  1768.             boolean had_char_class = false;
  1769.  
  1770.             if (p == pend) return REG_EBRACK;
  1771.  
  1772.             /* Ensure that we have enough space to push a charset: the
  1773.                opcode, the length count, and the bitset; 34 bytes in all.  */
  1774.         GET_BUFFER_SPACE (34);
  1775.  
  1776.             laststart = b;
  1777.  
  1778.             /* We test `*p == '^' twice, instead of using an if
  1779.                statement, so we only need one BUF_PUSH.  */
  1780.             BUF_PUSH (*p == '^' ? charset_not : charset); 
  1781.             if (*p == '^')
  1782.               p++;
  1783.  
  1784.             /* Remember the first position in the bracket expression.  */
  1785.             p1 = p;
  1786.  
  1787.             /* Push the number of bytes in the bitmap.  */
  1788.             BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  1789.  
  1790.             /* Clear the whole map.  */
  1791.             bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  1792.  
  1793.             /* charset_not matches newline according to a syntax bit.  */
  1794.             if ((re_opcode_t) b[-2] == charset_not
  1795.                 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
  1796.               SET_LIST_BIT ('\n');
  1797.  
  1798.             /* Read in characters and ranges, setting map bits.  */
  1799.             for (;;)
  1800.               {
  1801.                 if (p == pend) return REG_EBRACK;
  1802.  
  1803.                 PATFETCH (c);
  1804.  
  1805.                 /* \ might escape characters inside [...] and [^...].  */
  1806.                 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  1807.                   {
  1808.                     if (p == pend) return REG_EESCAPE;
  1809.  
  1810.                     PATFETCH (c1);
  1811.                     SET_LIST_BIT (c1);
  1812.                     continue;
  1813.                   }
  1814.  
  1815.                 /* Could be the end of the bracket expression.  If it's
  1816.                    not (i.e., when the bracket expression is `[]' so
  1817.                    far), the ']' character bit gets set way below.  */
  1818.                 if (c == ']' && p != p1 + 1)
  1819.                   break;
  1820.  
  1821.                 /* Look ahead to see if it's a range when the last thing
  1822.                    was a character class.  */
  1823.                 if (had_char_class && c == '-' && *p != ']')
  1824.                   return REG_ERANGE;
  1825.  
  1826.                 /* Look ahead to see if it's a range when the last thing
  1827.                    was a character: if this is a hyphen not at the
  1828.                    beginning or the end of a list, then it's the range
  1829.                    operator.  */
  1830.                 if (c == '-' 
  1831.                     && !(p - 2 >= pattern && p[-2] == '[') 
  1832.                     && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
  1833.                     && *p != ']')
  1834.                   {
  1835.                     reg_errcode_t ret
  1836.                       = compile_range (&p, pend, translate, syntax, b);
  1837.                     if (ret != REG_NOERROR) return ret;
  1838.                   }
  1839.  
  1840.                 else if (p[0] == '-' && p[1] != ']')
  1841.                   { /* This handles ranges made up of characters only.  */
  1842.                     reg_errcode_t ret;
  1843.  
  1844.             /* Move past the `-'.  */
  1845.                     PATFETCH (c1);
  1846.                     
  1847.                     ret = compile_range (&p, pend, translate, syntax, b);
  1848.                     if (ret != REG_NOERROR) return ret;
  1849.                   }
  1850.  
  1851.                 /* See if we're at the beginning of a possible character
  1852.                    class.  */
  1853.  
  1854.                 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
  1855.                   { /* Leave room for the null.  */
  1856.                     char str[CHAR_CLASS_MAX_LENGTH + 1];
  1857.  
  1858.                     PATFETCH (c);
  1859.                     c1 = 0;
  1860.  
  1861.                     /* If pattern is `[[:'.  */
  1862.                     if (p == pend) return REG_EBRACK;
  1863.  
  1864.                     for (;;)
  1865.                       {
  1866.                         PATFETCH (c);
  1867.                         if (c == ':' || c == ']' || p == pend
  1868.                             || c1 == CHAR_CLASS_MAX_LENGTH)
  1869.                           break;
  1870.                         str[c1++] = c;
  1871.                       }
  1872.                     str[c1] = '\0';
  1873.  
  1874.                     /* If isn't a word bracketed by `[:' and:`]':
  1875.                        undo the ending character, the letters, and leave 
  1876.                        the leading `:' and `[' (but set bits for them).  */
  1877.                     if (c == ':' && *p == ']')
  1878.                       {
  1879.                         int ch;
  1880.                         boolean is_alnum = STREQ (str, "alnum");
  1881.                         boolean is_alpha = STREQ (str, "alpha");
  1882.                         boolean is_blank = STREQ (str, "blank");
  1883.                         boolean is_cntrl = STREQ (str, "cntrl");
  1884.                         boolean is_digit = STREQ (str, "digit");
  1885.                         boolean is_graph = STREQ (str, "graph");
  1886.                         boolean is_lower = STREQ (str, "lower");
  1887.                         boolean is_print = STREQ (str, "print");
  1888.                         boolean is_punct = STREQ (str, "punct");
  1889.                         boolean is_space = STREQ (str, "space");
  1890.                         boolean is_upper = STREQ (str, "upper");
  1891.                         boolean is_xdigit = STREQ (str, "xdigit");
  1892.                         
  1893.                         if (!IS_CHAR_CLASS (str)) return REG_ECTYPE;
  1894.  
  1895.                         /* Throw away the ] at the end of the character
  1896.                            class.  */
  1897.                         PATFETCH (c);                    
  1898.  
  1899.                         if (p == pend) return REG_EBRACK;
  1900.  
  1901.                         for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
  1902.                           {
  1903.                             if (   (is_alnum  && ISALNUM (ch))
  1904.                                 || (is_alpha  && ISALPHA (ch))
  1905.                                 || (is_blank  && ISBLANK (ch))
  1906.                                 || (is_cntrl  && ISCNTRL (ch))
  1907.                                 || (is_digit  && ISDIGIT (ch))
  1908.                                 || (is_graph  && ISGRAPH (ch))
  1909.                                 || (is_lower  && ISLOWER (ch))
  1910.                                 || (is_print  && ISPRINT (ch))
  1911.                                 || (is_punct  && ISPUNCT (ch))
  1912.                                 || (is_space  && ISSPACE (ch))
  1913.                                 || (is_upper  && ISUPPER (ch))
  1914.                                 || (is_xdigit && ISXDIGIT (ch)))
  1915.                             SET_LIST_BIT (ch);
  1916.                           }
  1917.                         had_char_class = true;
  1918.                       }
  1919.                     else
  1920.                       {
  1921.                         c1++;
  1922.                         while (c1--)    
  1923.                           PATUNFETCH;
  1924.                         SET_LIST_BIT ('[');
  1925.                         SET_LIST_BIT (':');
  1926.                         had_char_class = false;
  1927.                       }
  1928.                   }
  1929.                 else
  1930.                   {
  1931.                     had_char_class = false;
  1932.                     SET_LIST_BIT (c);
  1933.                   }
  1934.               }
  1935.  
  1936.             /* Discard any (non)matching list bytes that are all 0 at the
  1937.                end of the map.  Decrease the map-length byte too.  */
  1938.             while ((int) b[-1] > 0 && b[b[-1] - 1] == 0) 
  1939.               b[-1]--; 
  1940.             b += b[-1];
  1941.           }
  1942.           break;
  1943.  
  1944.  
  1945.     case '(':
  1946.           if (syntax & RE_NO_BK_PARENS)
  1947.             goto handle_open;
  1948.           else
  1949.             goto normal_char;
  1950.  
  1951.  
  1952.         case ')':
  1953.           if (syntax & RE_NO_BK_PARENS)
  1954.             goto handle_close;
  1955.           else
  1956.             goto normal_char;
  1957.  
  1958.  
  1959.         case '\n':
  1960.           if (syntax & RE_NEWLINE_ALT)
  1961.             goto handle_alt;
  1962.           else
  1963.             goto normal_char;
  1964.  
  1965.  
  1966.     case '|':
  1967.           if (syntax & RE_NO_BK_VBAR)
  1968.             goto handle_alt;
  1969.           else
  1970.             goto normal_char;
  1971.  
  1972.  
  1973.         case '{':
  1974.            if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
  1975.              goto handle_interval;
  1976.            else
  1977.              goto normal_char;
  1978.  
  1979.  
  1980.         case '\\':
  1981.           if (p == pend) return REG_EESCAPE;
  1982.  
  1983.           /* Do not translate the character after the \, so that we can
  1984.              distinguish, e.g., \B from \b, even if we normally would
  1985.              translate, e.g., B to b.  */
  1986.           PATFETCH_RAW (c);
  1987.  
  1988.           switch (c)
  1989.             {
  1990.             case '(':
  1991.               if (syntax & RE_NO_BK_PARENS)
  1992.                 goto normal_backslash;
  1993.  
  1994.             handle_open:
  1995.               bufp->re_nsub++;
  1996.               regnum++;
  1997.  
  1998.               if (COMPILE_STACK_FULL)
  1999.                 { 
  2000.                   RETALLOC (compile_stack.stack, compile_stack.size << 1,
  2001.                             compile_stack_elt_t);
  2002.                   if (compile_stack.stack == NULL) return REG_ESPACE;
  2003.  
  2004.                   compile_stack.size <<= 1;
  2005.                 }
  2006.  
  2007.               /* These are the values to restore when we hit end of this
  2008.                  group.  They are all relative offsets, so that if the
  2009.                  whole pattern moves because of realloc, they will still
  2010.                  be valid.  */
  2011.               COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
  2012.               COMPILE_STACK_TOP.fixup_alt_jump 
  2013.                 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
  2014.               COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
  2015.               COMPILE_STACK_TOP.regnum = regnum;
  2016.  
  2017.               /* We will eventually replace the 0 with the number of
  2018.                  groups inner to this one.  But do not push a
  2019.                  start_memory for groups beyond the last one we can
  2020.                  represent in the compiled pattern.  */
  2021.               if (regnum <= MAX_REGNUM)
  2022.                 {
  2023.                   COMPILE_STACK_TOP.inner_group_offset = b - bufp->buffer + 2;
  2024.                   BUF_PUSH_3 (start_memory, regnum, 0);
  2025.                 }
  2026.                 
  2027.               compile_stack.avail++;
  2028.  
  2029.               fixup_alt_jump = 0;
  2030.               laststart = 0;
  2031.               begalt = b;
  2032.           /* If we've reached MAX_REGNUM groups, then this open
  2033.          won't actually generate any code, so we'll have to
  2034.          clear pending_exact explicitly.  */
  2035.           pending_exact = 0;
  2036.               break;
  2037.  
  2038.  
  2039.             case ')':
  2040.               if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
  2041.  
  2042.               if (COMPILE_STACK_EMPTY)
  2043.                 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  2044.                   goto normal_backslash;
  2045.                 else
  2046.                   return REG_ERPAREN;
  2047.  
  2048.             handle_close:
  2049.               if (fixup_alt_jump)
  2050.                 { /* Push a dummy failure point at the end of the
  2051.                      alternative for a possible future
  2052.                      `pop_failure_jump' to pop.  See comments at
  2053.                      `push_dummy_failure' in `re_match_2'.  */
  2054.                   BUF_PUSH (push_dummy_failure);
  2055.                   
  2056.                   /* We allocated space for this jump when we assigned
  2057.                      to `fixup_alt_jump', in the `handle_alt' case below.  */
  2058.                   STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1);
  2059.                 }
  2060.  
  2061.               /* See similar code for backslashed left paren above.  */
  2062.               if (COMPILE_STACK_EMPTY)
  2063.                 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  2064.                   goto normal_char;
  2065.                 else
  2066.                   return REG_ERPAREN;
  2067.  
  2068.               /* Since we just checked for an empty stack above, this
  2069.                  ``can't happen''.  */
  2070.               assert (compile_stack.avail != 0);
  2071.               {
  2072.                 /* We don't just want to restore into `regnum', because
  2073.                    later groups should continue to be numbered higher,
  2074.                    as in `(ab)c(de)' -- the second group is #2.  */
  2075.                 regnum_t this_group_regnum;
  2076.  
  2077.                 compile_stack.avail--;        
  2078.                 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
  2079.                 fixup_alt_jump
  2080.                   = COMPILE_STACK_TOP.fixup_alt_jump
  2081.                     ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1 
  2082.                     : 0;
  2083.                 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
  2084.                 this_group_regnum = COMPILE_STACK_TOP.regnum;
  2085.         /* If we've reached MAX_REGNUM groups, then this open
  2086.            won't actually generate any code, so we'll have to
  2087.            clear pending_exact explicitly.  */
  2088.         pending_exact = 0;
  2089.  
  2090.                 /* We're at the end of the group, so now we know how many
  2091.                    groups were inside this one.  */
  2092.                 if (this_group_regnum <= MAX_REGNUM)
  2093.                   {
  2094.                     unsigned char *inner_group_loc
  2095.                       = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset;
  2096.                     
  2097.                     *inner_group_loc = regnum - this_group_regnum;
  2098.                     BUF_PUSH_3 (stop_memory, this_group_regnum,
  2099.                                 regnum - this_group_regnum);
  2100.                   }
  2101.               }
  2102.               break;
  2103.  
  2104.  
  2105.             case '|':                    /* `\|'.  */
  2106.               if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
  2107.                 goto normal_backslash;
  2108.             handle_alt:
  2109.               if (syntax & RE_LIMITED_OPS)
  2110.                 goto normal_char;
  2111.  
  2112.               /* Insert before the previous alternative a jump which
  2113.                  jumps to this alternative if the former fails.  */
  2114.               GET_BUFFER_SPACE (3);
  2115.               INSERT_JUMP (on_failure_jump, begalt, b + 6);
  2116.               pending_exact = 0;
  2117.               b += 3;
  2118.  
  2119.               /* The alternative before this one has a jump after it
  2120.                  which gets executed if it gets matched.  Adjust that
  2121.                  jump so it will jump to this alternative's analogous
  2122.                  jump (put in below, which in turn will jump to the next
  2123.                  (if any) alternative's such jump, etc.).  The last such
  2124.                  jump jumps to the correct final destination.  A picture:
  2125.                           _____ _____ 
  2126.                           |   | |   |   
  2127.                           |   v |   v 
  2128.                          a | b   | c   
  2129.  
  2130.                  If we are at `b', then fixup_alt_jump right now points to a
  2131.                  three-byte space after `a'.  We'll put in the jump, set
  2132.                  fixup_alt_jump to right after `b', and leave behind three
  2133.                  bytes which we'll fill in when we get to after `c'.  */
  2134.  
  2135.               if (fixup_alt_jump)
  2136.                 STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  2137.  
  2138.               /* Mark and leave space for a jump after this alternative,
  2139.                  to be filled in later either by next alternative or
  2140.                  when know we're at the end of a series of alternatives.  */
  2141.               fixup_alt_jump = b;
  2142.               GET_BUFFER_SPACE (3);
  2143.               b += 3;
  2144.  
  2145.               laststart = 0;
  2146.               begalt = b;
  2147.               break;
  2148.  
  2149.  
  2150.             case '{': 
  2151.               /* If \{ is a literal.  */
  2152.               if (!(syntax & RE_INTERVALS)
  2153.                      /* If we're at `\{' and it's not the open-interval 
  2154.                         operator.  */
  2155.                   || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  2156.                   || (p - 2 == pattern  &&  p == pend))
  2157.                 goto normal_backslash;
  2158.  
  2159.             handle_interval:
  2160.               {
  2161.                 /* If got here, then the syntax allows intervals.  */
  2162.  
  2163.                 /* At least (most) this many matches must be made.  */
  2164.                 int lower_bound = -1, upper_bound = -1;
  2165.  
  2166.                 beg_interval = p - 1;
  2167.  
  2168.                 if (p == pend)
  2169.                   {
  2170.                     if (syntax & RE_NO_BK_BRACES)
  2171.                       goto unfetch_interval;
  2172.                     else
  2173.                       return REG_EBRACE;
  2174.                   }
  2175.  
  2176.                 GET_UNSIGNED_NUMBER (lower_bound);
  2177.  
  2178.                 if (c == ',')
  2179.                   {
  2180.                     GET_UNSIGNED_NUMBER (upper_bound);
  2181.                     if (upper_bound < 0) upper_bound = RE_DUP_MAX;
  2182.                   }
  2183.                 else
  2184.                   /* Interval such as `{1}' => match exactly once. */
  2185.                   upper_bound = lower_bound;
  2186.  
  2187.                 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  2188.                     || lower_bound > upper_bound)
  2189.                   {
  2190.                     if (syntax & RE_NO_BK_BRACES)
  2191.                       goto unfetch_interval;
  2192.                     else 
  2193.                       return REG_BADBR;
  2194.                   }
  2195.  
  2196.                 if (!(syntax & RE_NO_BK_BRACES)) 
  2197.                   {
  2198.                     if (c != '\\') return REG_EBRACE;
  2199.  
  2200.                     PATFETCH (c);
  2201.                   }
  2202.  
  2203.                 if (c != '}')
  2204.                   {
  2205.                     if (syntax & RE_NO_BK_BRACES)
  2206.                       goto unfetch_interval;
  2207.                     else 
  2208.                       return REG_BADBR;
  2209.                   }
  2210.  
  2211.                 /* We just parsed a valid interval.  */
  2212.  
  2213.                 /* If it's invalid to have no preceding re.  */
  2214.                 if (!laststart)
  2215.                   {
  2216.                     if (syntax & RE_CONTEXT_INVALID_OPS)
  2217.                       return REG_BADRPT;
  2218.                     else if (syntax & RE_CONTEXT_INDEP_OPS)
  2219.                       laststart = b;
  2220.                     else
  2221.                       goto unfetch_interval;
  2222.                   }
  2223.  
  2224.                 /* If the upper bound is zero, don't want to succeed at
  2225.                    all; jump from `laststart' to `b + 3', which will be
  2226.                    the end of the buffer after we insert the jump.  */
  2227.                  if (upper_bound == 0)
  2228.                    {
  2229.                      GET_BUFFER_SPACE (3);
  2230.                      INSERT_JUMP (jump, laststart, b + 3);
  2231.                      b += 3;
  2232.                    }
  2233.  
  2234.                  /* Otherwise, we have a nontrivial interval.  When
  2235.                     we're all done, the pattern will look like:
  2236.                       set_number_at <jump count> <upper bound>
  2237.                       set_number_at <succeed_n count> <lower bound>
  2238.                       succeed_n <after jump addr> <succed_n count>
  2239.                       <body of loop>
  2240.                       jump_n <succeed_n addr> <jump count>
  2241.                     (The upper bound and `jump_n' are omitted if
  2242.                     `upper_bound' is 1, though.)  */
  2243.                  else 
  2244.                    { /* If the upper bound is > 1, we need to insert
  2245.                         more at the end of the loop.  */
  2246.                      unsigned nbytes = 10 + (upper_bound > 1) * 10;
  2247.  
  2248.                      GET_BUFFER_SPACE (nbytes);
  2249.  
  2250.                      /* Initialize lower bound of the `succeed_n', even
  2251.                         though it will be set during matching by its
  2252.                         attendant `set_number_at' (inserted next),
  2253.                         because `re_compile_fastmap' needs to know.
  2254.                         Jump to the `jump_n' we might insert below.  */
  2255.                      INSERT_JUMP2 (succeed_n, laststart,
  2256.                                    b + 5 + (upper_bound > 1) * 5,
  2257.                                    lower_bound);
  2258.                      b += 5;
  2259.  
  2260.                      /* Code to initialize the lower bound.  Insert 
  2261.                         before the `succeed_n'.  The `5' is the last two
  2262.                         bytes of this `set_number_at', plus 3 bytes of
  2263.                         the following `succeed_n'.  */
  2264.                      insert_op2 (set_number_at, laststart, 5, lower_bound, b);
  2265.                      b += 5;
  2266.  
  2267.                      if (upper_bound > 1)
  2268.                        { /* More than one repetition is allowed, so
  2269.                             append a backward jump to the `succeed_n'
  2270.                             that starts this interval.
  2271.                             
  2272.                             When we've reached this during matching,
  2273.                             we'll have matched the interval once, so
  2274.                             jump back only `upper_bound - 1' times.  */
  2275.                          STORE_JUMP2 (jump_n, b, laststart + 5,
  2276.                                       upper_bound - 1);
  2277.                          b += 5;
  2278.  
  2279.                          /* The location we want to set is the second
  2280.                             parameter of the `jump_n'; that is `b-2' as
  2281.                             an absolute address.  `laststart' will be
  2282.                             the `set_number_at' we're about to insert;
  2283.                             `laststart+3' the number to set, the source
  2284.                             for the relative address.  But we are
  2285.                             inserting into the middle of the pattern --
  2286.                             so everything is getting moved up by 5.
  2287.                             Conclusion: (b - 2) - (laststart + 3) + 5,
  2288.                             i.e., b - laststart.
  2289.                             
  2290.                             We insert this at the beginning of the loop
  2291.                             so that if we fail during matching, we'll
  2292.                             reinitialize the bounds.  */
  2293.                          insert_op2 (set_number_at, laststart, b - laststart,
  2294.                                      upper_bound - 1, b);
  2295.                          b += 5;
  2296.                        }
  2297.                    }
  2298.                 pending_exact = 0;
  2299.                 beg_interval = NULL;
  2300.               }
  2301.               break;
  2302.  
  2303.             unfetch_interval:
  2304.               /* If an invalid interval, match the characters as literals.  */
  2305.                assert (beg_interval);
  2306.                p = beg_interval;
  2307.                beg_interval = NULL;
  2308.  
  2309.                /* normal_char and normal_backslash need `c'.  */
  2310.                PATFETCH (c);    
  2311.  
  2312.                if (!(syntax & RE_NO_BK_BRACES))
  2313.                  {
  2314.                    if (p > pattern  &&  p[-1] == '\\')
  2315.                      goto normal_backslash;
  2316.                  }
  2317.                goto normal_char;
  2318.  
  2319. #ifdef emacs
  2320.             /* There is no way to specify the before_dot and after_dot
  2321.                operators.  rms says this is ok.  --karl  */
  2322.             case '=':
  2323.               BUF_PUSH (at_dot);
  2324.               break;
  2325.  
  2326.             case 's':    
  2327.               laststart = b;
  2328.               PATFETCH (c);
  2329.               BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
  2330.               break;
  2331.  
  2332.             case 'S':
  2333.               laststart = b;
  2334.               PATFETCH (c);
  2335.               BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
  2336.               break;
  2337. #endif /* emacs */
  2338.  
  2339.  
  2340.             case 'w':
  2341.               laststart = b;
  2342.               BUF_PUSH (wordchar);
  2343.               break;
  2344.  
  2345.  
  2346.             case 'W':
  2347.               laststart = b;
  2348.               BUF_PUSH (notwordchar);
  2349.               break;
  2350.  
  2351.  
  2352.             case '<':
  2353.               BUF_PUSH (wordbeg);
  2354.               break;
  2355.  
  2356.             case '>':
  2357.               BUF_PUSH (wordend);
  2358.               break;
  2359.  
  2360.             case 'b':
  2361.               BUF_PUSH (wordbound);
  2362.               break;
  2363.  
  2364.             case 'B':
  2365.               BUF_PUSH (notwordbound);
  2366.               break;
  2367.  
  2368.             case '`':
  2369.               BUF_PUSH (begbuf);
  2370.               break;
  2371.  
  2372.             case '\'':
  2373.               BUF_PUSH (endbuf);
  2374.               break;
  2375.  
  2376.             case '1': case '2': case '3': case '4': case '5':
  2377.             case '6': case '7': case '8': case '9':
  2378.               if (syntax & RE_NO_BK_REFS)
  2379.                 goto normal_char;
  2380.  
  2381.               c1 = c - '0';
  2382.  
  2383.               if (c1 > regnum)
  2384.                 return REG_ESUBREG;
  2385.  
  2386.               /* Can't back reference to a subexpression if inside of it.  */
  2387.               if (group_in_compile_stack (compile_stack, c1))
  2388.                 goto normal_char;
  2389.  
  2390.               laststart = b;
  2391.               BUF_PUSH_2 (duplicate, c1);
  2392.               break;
  2393.  
  2394.  
  2395.             case '+':
  2396.             case '?':
  2397.               if (syntax & RE_BK_PLUS_QM)
  2398.                 goto handle_plus;
  2399.               else
  2400.                 goto normal_backslash;
  2401.  
  2402.             default:
  2403.             normal_backslash:
  2404.               /* You might think it would be useful for \ to mean
  2405.                  not to translate; but if we don't translate it
  2406.                  it will never match anything.  */
  2407.               c = TRANSLATE (c);
  2408.               goto normal_char;
  2409.             }
  2410.           break;
  2411.  
  2412.  
  2413.     default:
  2414.         /* Expects the character in `c'.  */
  2415.     normal_char:
  2416.           /* If no exactn currently being built.  */
  2417.           if (!pending_exact 
  2418.  
  2419.               /* If last exactn not at current position.  */
  2420.               || pending_exact + *pending_exact + 1 != b
  2421.               
  2422.               /* We have only one byte following the exactn for the count.  */
  2423.           || *pending_exact == (1 << BYTEWIDTH) - 1
  2424.  
  2425.               /* If followed by a repetition operator.  */
  2426.               || *p == '*' || *p == '^'
  2427.           || ((syntax & RE_BK_PLUS_QM)
  2428.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  2429.           : (*p == '+' || *p == '?'))
  2430.           || ((syntax & RE_INTERVALS)
  2431.                   && ((syntax & RE_NO_BK_BRACES)
  2432.               ? *p == '{'
  2433.                       : (p[0] == '\\' && p[1] == '{'))))
  2434.         {
  2435.           /* Start building a new exactn.  */
  2436.               
  2437.               laststart = b;
  2438.  
  2439.           BUF_PUSH_2 (exactn, 0);
  2440.           pending_exact = b - 1;
  2441.             }
  2442.             
  2443.       BUF_PUSH (c);
  2444.           (*pending_exact)++;
  2445.       break;
  2446.         } /* switch (c) */
  2447.     } /* while p != pend */
  2448.  
  2449.   
  2450.   /* Through the pattern now.  */
  2451.   
  2452.   if (fixup_alt_jump)
  2453.     STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  2454.  
  2455.   if (!COMPILE_STACK_EMPTY) 
  2456.     return REG_EPAREN;
  2457.  
  2458.   free (compile_stack.stack);
  2459.  
  2460.   /* We have succeeded; set the length of the buffer.  */
  2461.   bufp->used = b - bufp->buffer;
  2462.  
  2463. #ifdef DEBUG
  2464.   if (debug)
  2465.     {
  2466.       DEBUG_PRINT1 ("\nCompiled pattern: \n");
  2467.       print_compiled_pattern (bufp);
  2468.     }
  2469. #endif /* DEBUG */
  2470.  
  2471. #ifndef MATCH_MAY_ALLOCATE
  2472.   /* Initialize the failure stack to the largest possible stack.  This
  2473.      isn't necessary unless we're trying to avoid calling alloca in
  2474.      the search and match routines.  */
  2475.   {
  2476.     int num_regs = bufp->re_nsub + 1;
  2477.  
  2478.     /* Since DOUBLE_FAIL_STACK refuses to double only if the current size
  2479.        is strictly greater than re_max_failures, the largest possible stack
  2480.        is 2 * re_max_failures failure points.  */
  2481.     fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS);
  2482.     if (fail_stack.stack)
  2483.       fail_stack.stack =
  2484.     (fail_stack_elt_t *) realloc (fail_stack.stack,
  2485.                       (fail_stack.size
  2486.                        * sizeof (fail_stack_elt_t)));
  2487.     else
  2488.       fail_stack.stack =
  2489.     (fail_stack_elt_t *) malloc (fail_stack.size 
  2490.                      * sizeof (fail_stack_elt_t));
  2491.  
  2492.     /* Initialize some other variables the matcher uses.  */
  2493.     RETALLOC_IF (regstart,     num_regs, const char *);
  2494.     RETALLOC_IF (regend,     num_regs, const char *);
  2495.     RETALLOC_IF (old_regstart,     num_regs, const char *);
  2496.     RETALLOC_IF (old_regend,     num_regs, const char *);
  2497.     RETALLOC_IF (best_regstart,  num_regs, const char *);
  2498.     RETALLOC_IF (best_regend,     num_regs, const char *);
  2499.     RETALLOC_IF (reg_info,     num_regs, register_info_type);
  2500.     RETALLOC_IF (reg_dummy,     num_regs, const char *);
  2501.     RETALLOC_IF (reg_info_dummy, num_regs, register_info_type);
  2502.   }
  2503. #endif
  2504.  
  2505.   return REG_NOERROR;
  2506. } /* regex_compile */
  2507.  
  2508. /* Subroutines for `regex_compile'.  */
  2509.  
  2510. /* Store OP at LOC followed by two-byte integer parameter ARG.  */
  2511.  
  2512. static void
  2513. store_op1 (op, loc, arg)
  2514.     re_opcode_t op;
  2515.     unsigned char *loc;
  2516.     int arg;
  2517. {
  2518.   *loc = (unsigned char) op;
  2519.   STORE_NUMBER (loc + 1, arg);
  2520. }
  2521.  
  2522.  
  2523. /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2.  */
  2524.  
  2525. static void
  2526. store_op2 (op, loc, arg1, arg2)
  2527.     re_opcode_t op;
  2528.     unsigned char *loc;
  2529.     int arg1, arg2;
  2530. {
  2531.   *loc = (unsigned char) op;
  2532.   STORE_NUMBER (loc + 1, arg1);
  2533.   STORE_NUMBER (loc + 3, arg2);
  2534. }
  2535.  
  2536.  
  2537. /* Copy the bytes from LOC to END to open up three bytes of space at LOC
  2538.    for OP followed by two-byte integer parameter ARG.  */
  2539.  
  2540. static void
  2541. insert_op1 (op, loc, arg, end)
  2542.     re_opcode_t op;
  2543.     unsigned char *loc;
  2544.     int arg;
  2545.     unsigned char *end;    
  2546. {
  2547.   register unsigned char *pfrom = end;
  2548.   register unsigned char *pto = end + 3;
  2549.  
  2550.   while (pfrom != loc)
  2551.     *--pto = *--pfrom;
  2552.     
  2553.   store_op1 (op, loc, arg);
  2554. }
  2555.  
  2556.  
  2557. /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2.  */
  2558.  
  2559. static void
  2560. insert_op2 (op, loc, arg1, arg2, end)
  2561.     re_opcode_t op;
  2562.     unsigned char *loc;
  2563.     int arg1, arg2;
  2564.     unsigned char *end;    
  2565. {
  2566.   register unsigned char *pfrom = end;
  2567.   register unsigned char *pto = end + 5;
  2568.  
  2569.   while (pfrom != loc)
  2570.     *--pto = *--pfrom;
  2571.     
  2572.   store_op2 (op, loc, arg1, arg2);
  2573. }
  2574.  
  2575.  
  2576. /* P points to just after a ^ in PATTERN.  Return true if that ^ comes
  2577.    after an alternative or a begin-subexpression.  We assume there is at
  2578.    least one character before the ^.  */
  2579.  
  2580. static boolean
  2581. at_begline_loc_p (pattern, p, syntax)
  2582.     const char *pattern, *p;
  2583.     reg_syntax_t syntax;
  2584. {
  2585.   const char *prev = p - 2;
  2586.   boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
  2587.   
  2588.   return
  2589.        /* After a subexpression?  */
  2590.        (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
  2591.        /* After an alternative?  */
  2592.     || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
  2593. }
  2594.  
  2595.  
  2596. /* The dual of at_begline_loc_p.  This one is for $.  We assume there is
  2597.    at least one character after the $, i.e., `P < PEND'.  */
  2598.  
  2599. static boolean
  2600. at_endline_loc_p (p, pend, syntax)
  2601.     const char *p, *pend;
  2602.     int syntax;
  2603. {
  2604.   const char *next = p;
  2605.   boolean next_backslash = *next == '\\';
  2606.   const char *next_next = p + 1 < pend ? p + 1 : NULL;
  2607.   
  2608.   return
  2609.        /* Before a subexpression?  */
  2610.        (syntax & RE_NO_BK_PARENS ? *next == ')'
  2611.         : next_backslash && next_next && *next_next == ')')
  2612.        /* Before an alternative?  */
  2613.     || (syntax & RE_NO_BK_VBAR ? *next == '|'
  2614.         : next_backslash && next_next && *next_next == '|');
  2615. }
  2616.  
  2617.  
  2618. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and 
  2619.    false if it's not.  */
  2620.  
  2621. static boolean
  2622. group_in_compile_stack (compile_stack, regnum)
  2623.     compile_stack_type compile_stack;
  2624.     regnum_t regnum;
  2625. {
  2626.   int this_element;
  2627.  
  2628.   for (this_element = compile_stack.avail - 1;  
  2629.        this_element >= 0; 
  2630.        this_element--)
  2631.     if (compile_stack.stack[this_element].regnum == regnum)
  2632.       return true;
  2633.  
  2634.   return false;
  2635. }
  2636.  
  2637.  
  2638. /* Read the ending character of a range (in a bracket expression) from the
  2639.    uncompiled pattern *P_PTR (which ends at PEND).  We assume the
  2640.    starting character is in `P[-2]'.  (`P[-1]' is the character `-'.)
  2641.    Then we set the translation of all bits between the starting and
  2642.    ending characters (inclusive) in the compiled pattern B.
  2643.    
  2644.    Return an error code.
  2645.    
  2646.    We use these short variable names so we can use the same macros as
  2647.    `regex_compile' itself.  */
  2648.  
  2649. static reg_errcode_t
  2650. compile_range (p_ptr, pend, translate, syntax, b)
  2651.     const char **p_ptr, *pend;
  2652.     char *translate;
  2653.     reg_syntax_t syntax;
  2654.     unsigned char *b;
  2655. {
  2656.   unsigned this_char;
  2657.  
  2658.   const char *p = *p_ptr;
  2659.   int range_start, range_end;
  2660.   
  2661.   if (p == pend)
  2662.     return REG_ERANGE;
  2663.  
  2664.   /* Even though the pattern is a signed `char *', we need to fetch
  2665.      with unsigned char *'s; if the high bit of the pattern character
  2666.      is set, the range endpoints will be negative if we fetch using a
  2667.      signed char *.
  2668.  
  2669.      We also want to fetch the endpoints without translating them; the 
  2670.      appropriate translation is done in the bit-setting loop below.  */
  2671.   range_start = ((unsigned char *) p)[-2];
  2672.   range_end   = ((unsigned char *) p)[0];
  2673.  
  2674.   /* Have to increment the pointer into the pattern string, so the
  2675.      caller isn't still at the ending character.  */
  2676.   (*p_ptr)++;
  2677.  
  2678.   /* If the start is after the end, the range is empty.  */
  2679.   if (range_start > range_end)
  2680.     return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
  2681.  
  2682.   /* Here we see why `this_char' has to be larger than an `unsigned
  2683.      char' -- the range is inclusive, so if `range_end' == 0xff
  2684.      (assuming 8-bit characters), we would otherwise go into an infinite
  2685.      loop, since all characters <= 0xff.  */
  2686.   for (this_char = range_start; this_char <= range_end; this_char++)
  2687.     {
  2688.       SET_LIST_BIT (TRANSLATE (this_char));
  2689.     }
  2690.   
  2691.   return REG_NOERROR;
  2692. }
  2693.  
  2694. /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
  2695.    BUFP.  A fastmap records which of the (1 << BYTEWIDTH) possible
  2696.    characters can start a string that matches the pattern.  This fastmap
  2697.    is used by re_search to skip quickly over impossible starting points.
  2698.  
  2699.    The caller must supply the address of a (1 << BYTEWIDTH)-byte data
  2700.    area as BUFP->fastmap.
  2701.    
  2702.    We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
  2703.    the pattern buffer.
  2704.  
  2705.    Returns 0 if we succeed, -2 if an internal error.   */
  2706.  
  2707. int
  2708. re_compile_fastmap (bufp)
  2709.      struct re_pattern_buffer *bufp;
  2710. {
  2711.   int j, k;
  2712. #ifdef MATCH_MAY_ALLOCATE
  2713.   fail_stack_type fail_stack;
  2714. #endif
  2715. #ifndef REGEX_MALLOC
  2716.   char *destination;
  2717. #endif
  2718.   /* We don't push any register information onto the failure stack.  */
  2719.   unsigned num_regs = 0;
  2720.   
  2721.   register char *fastmap = bufp->fastmap;
  2722.   unsigned char *pattern = bufp->buffer;
  2723.   unsigned long size = bufp->used;
  2724.   const unsigned char *p = pattern;
  2725.   register unsigned char *pend = pattern + size;
  2726.  
  2727.   /* Assume that each path through the pattern can be null until
  2728.      proven otherwise.  We set this false at the bottom of switch
  2729.      statement, to which we get only if a particular path doesn't
  2730.      match the empty string.  */
  2731.   boolean path_can_be_null = true;
  2732.  
  2733.   /* We aren't doing a `succeed_n' to begin with.  */
  2734.   boolean succeed_n_p = false;
  2735.  
  2736.   assert (fastmap != NULL && p != NULL);
  2737.   
  2738.   INIT_FAIL_STACK ();
  2739.   bzero (fastmap, 1 << BYTEWIDTH);  /* Assume nothing's valid.  */
  2740.   bufp->fastmap_accurate = 1;        /* It will be when we're done.  */
  2741.   bufp->can_be_null = 0;
  2742.       
  2743.   while (p != pend || !FAIL_STACK_EMPTY ())
  2744.     {
  2745.       if (p == pend)
  2746.         {
  2747.           bufp->can_be_null |= path_can_be_null;
  2748.           
  2749.           /* Reset for next path.  */
  2750.           path_can_be_null = true;
  2751.           
  2752.           p = fail_stack.stack[--fail_stack.avail];
  2753.     }
  2754.  
  2755.       /* We should never be about to go beyond the end of the pattern.  */
  2756.       assert (p < pend);
  2757.       
  2758. #ifdef SWITCH_ENUM_BUG
  2759.       switch ((int) ((re_opcode_t) *p++))
  2760. #else
  2761.       switch ((re_opcode_t) *p++)
  2762. #endif
  2763.     {
  2764.  
  2765.         /* I guess the idea here is to simply not bother with a fastmap
  2766.            if a backreference is used, since it's too hard to figure out
  2767.            the fastmap for the corresponding group.  Setting
  2768.            `can_be_null' stops `re_search_2' from using the fastmap, so
  2769.            that is all we do.  */
  2770.     case duplicate:
  2771.       bufp->can_be_null = 1;
  2772.           return 0;
  2773.  
  2774.  
  2775.       /* Following are the cases which match a character.  These end
  2776.          with `break'.  */
  2777.  
  2778.     case exactn:
  2779.           fastmap[p[1]] = 1;
  2780.       break;
  2781.  
  2782.  
  2783.         case charset:
  2784.           for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  2785.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  2786.               fastmap[j] = 1;
  2787.       break;
  2788.  
  2789.  
  2790.     case charset_not:
  2791.       /* Chars beyond end of map must be allowed.  */
  2792.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  2793.             fastmap[j] = 1;
  2794.  
  2795.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  2796.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  2797.               fastmap[j] = 1;
  2798.           break;
  2799.  
  2800.  
  2801.     case wordchar:
  2802.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2803.         if (SYNTAX (j) == Sword)
  2804.           fastmap[j] = 1;
  2805.       break;
  2806.  
  2807.  
  2808.     case notwordchar:
  2809.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2810.         if (SYNTAX (j) != Sword)
  2811.           fastmap[j] = 1;
  2812.       break;
  2813.  
  2814.  
  2815.         case anychar:
  2816.           /* `.' matches anything ...  */
  2817.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2818.             fastmap[j] = 1;
  2819.  
  2820.           /* ... except perhaps newline.  */
  2821.           if (!(bufp->syntax & RE_DOT_NEWLINE))
  2822.             fastmap['\n'] = 0;
  2823.  
  2824.           /* Return if we have already set `can_be_null'; if we have,
  2825.              then the fastmap is irrelevant.  Something's wrong here.  */
  2826.       else if (bufp->can_be_null)
  2827.         return 0;
  2828.  
  2829.           /* Otherwise, have to check alternative paths.  */
  2830.       break;
  2831.  
  2832.  
  2833. #ifdef emacs
  2834.         case syntaxspec:
  2835.       k = *p++;
  2836.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2837.         if (SYNTAX (j) == (enum syntaxcode) k)
  2838.           fastmap[j] = 1;
  2839.       break;
  2840.  
  2841.  
  2842.     case notsyntaxspec:
  2843.       k = *p++;
  2844.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2845.         if (SYNTAX (j) != (enum syntaxcode) k)
  2846.           fastmap[j] = 1;
  2847.       break;
  2848.  
  2849.  
  2850.       /* All cases after this match the empty string.  These end with
  2851.          `continue'.  */
  2852.  
  2853.  
  2854.     case before_dot:
  2855.     case at_dot:
  2856.     case after_dot:
  2857.           continue;
  2858. #endif /* not emacs */
  2859.  
  2860.  
  2861.         case no_op:
  2862.         case begline:
  2863.         case endline:
  2864.     case begbuf:
  2865.     case endbuf:
  2866.     case wordbound:
  2867.     case notwordbound:
  2868.     case wordbeg:
  2869.     case wordend:
  2870.         case push_dummy_failure:
  2871.           continue;
  2872.  
  2873.  
  2874.     case jump_n:
  2875.         case pop_failure_jump:
  2876.     case maybe_pop_jump:
  2877.     case jump:
  2878.         case jump_past_alt:
  2879.     case dummy_failure_jump:
  2880.           EXTRACT_NUMBER_AND_INCR (j, p);
  2881.       p += j;    
  2882.       if (j > 0)
  2883.         continue;
  2884.             
  2885.           /* Jump backward implies we just went through the body of a
  2886.              loop and matched nothing.  Opcode jumped to should be
  2887.              `on_failure_jump' or `succeed_n'.  Just treat it like an
  2888.              ordinary jump.  For a * loop, it has pushed its failure
  2889.              point already; if so, discard that as redundant.  */
  2890.           if ((re_opcode_t) *p != on_failure_jump
  2891.           && (re_opcode_t) *p != succeed_n)
  2892.         continue;
  2893.  
  2894.           p++;
  2895.           EXTRACT_NUMBER_AND_INCR (j, p);
  2896.           p += j;        
  2897.       
  2898.           /* If what's on the stack is where we are now, pop it.  */
  2899.           if (!FAIL_STACK_EMPTY () 
  2900.           && fail_stack.stack[fail_stack.avail - 1] == p)
  2901.             fail_stack.avail--;
  2902.  
  2903.           continue;
  2904.  
  2905.  
  2906.         case on_failure_jump:
  2907.         case on_failure_keep_string_jump:
  2908.     handle_on_failure_jump:
  2909.           EXTRACT_NUMBER_AND_INCR (j, p);
  2910.  
  2911.           /* For some patterns, e.g., `(a?)?', `p+j' here points to the
  2912.              end of the pattern.  We don't want to push such a point,
  2913.              since when we restore it above, entering the switch will
  2914.              increment `p' past the end of the pattern.  We don't need
  2915.              to push such a point since we obviously won't find any more
  2916.              fastmap entries beyond `pend'.  Such a pattern can match
  2917.              the null string, though.  */
  2918.           if (p + j < pend)
  2919.             {
  2920.               if (!PUSH_PATTERN_OP (p + j, fail_stack))
  2921.                 return -2;
  2922.             }
  2923.           else
  2924.             bufp->can_be_null = 1;
  2925.  
  2926.           if (succeed_n_p)
  2927.             {
  2928.               EXTRACT_NUMBER_AND_INCR (k, p);    /* Skip the n.  */
  2929.               succeed_n_p = false;
  2930.         }
  2931.  
  2932.           continue;
  2933.  
  2934.  
  2935.     case succeed_n:
  2936.           /* Get to the number of times to succeed.  */
  2937.           p += 2;        
  2938.  
  2939.           /* Increment p past the n for when k != 0.  */
  2940.           EXTRACT_NUMBER_AND_INCR (k, p);
  2941.           if (k == 0)
  2942.         {
  2943.               p -= 4;
  2944.             succeed_n_p = true;  /* Spaghetti code alert.  */
  2945.               goto handle_on_failure_jump;
  2946.             }
  2947.           continue;
  2948.  
  2949.  
  2950.     case set_number_at:
  2951.           p += 4;
  2952.           continue;
  2953.  
  2954.  
  2955.     case start_memory:
  2956.         case stop_memory:
  2957.       p += 2;
  2958.       continue;
  2959.  
  2960.  
  2961.     default:
  2962.           abort (); /* We have listed all the cases.  */
  2963.         } /* switch *p++ */
  2964.  
  2965.       /* Getting here means we have found the possible starting
  2966.          characters for one path of the pattern -- and that the empty
  2967.          string does not match.  We need not follow this path further.
  2968.          Instead, look at the next alternative (remembered on the
  2969.          stack), or quit if no more.  The test at the top of the loop
  2970.          does these things.  */
  2971.       path_can_be_null = false;
  2972.       p = pend;
  2973.     } /* while p */
  2974.  
  2975.   /* Set `can_be_null' for the last path (also the first path, if the
  2976.      pattern is empty).  */
  2977.   bufp->can_be_null |= path_can_be_null;
  2978.   return 0;
  2979. } /* re_compile_fastmap */
  2980.  
  2981. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  2982.    ENDS.  Subsequent matches using PATTERN_BUFFER and REGS will use
  2983.    this memory for recording register information.  STARTS and ENDS
  2984.    must be allocated using the malloc library routine, and must each
  2985.    be at least NUM_REGS * sizeof (regoff_t) bytes long.
  2986.  
  2987.    If NUM_REGS == 0, then subsequent matches should allocate their own
  2988.    register data.
  2989.  
  2990.    Unless this function is called, the first search or match using
  2991.    PATTERN_BUFFER will allocate its own register data, without
  2992.    freeing the old data.  */
  2993.  
  2994. void
  2995. re_set_registers (bufp, regs, num_regs, starts, ends)
  2996.     struct re_pattern_buffer *bufp;
  2997.     struct re_registers *regs;
  2998.     unsigned num_regs;
  2999.     regoff_t *starts, *ends;
  3000. {
  3001.   if (num_regs)
  3002.     {
  3003.       bufp->regs_allocated = REGS_REALLOCATE;
  3004.       regs->num_regs = num_regs;
  3005.       regs->start = starts;
  3006.       regs->end = ends;
  3007.     }
  3008.   else
  3009.     {
  3010.       bufp->regs_allocated = REGS_UNALLOCATED;
  3011.       regs->num_regs = 0;
  3012.       regs->start = regs->end = (regoff_t) 0;
  3013.     }
  3014. }
  3015.  
  3016. /* Searching routines.  */
  3017.  
  3018. /* Like re_search_2, below, but only one string is specified, and
  3019.    doesn't let you say where to stop matching. */
  3020.  
  3021. int
  3022. re_search (bufp, string, size, startpos, range, regs)
  3023.      struct re_pattern_buffer *bufp;
  3024.      const char *string;
  3025.      int size, startpos, range;
  3026.      struct re_registers *regs;
  3027. {
  3028.   return re_search_2 (bufp, NULL, 0, string, size, startpos, range, 
  3029.               regs, size);
  3030. }
  3031.  
  3032.  
  3033. /* Using the compiled pattern in BUFP->buffer, first tries to match the
  3034.    virtual concatenation of STRING1 and STRING2, starting first at index
  3035.    STARTPOS, then at STARTPOS + 1, and so on.
  3036.    
  3037.    STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
  3038.    
  3039.    RANGE is how far to scan while trying to match.  RANGE = 0 means try
  3040.    only at STARTPOS; in general, the last start tried is STARTPOS +
  3041.    RANGE.
  3042.    
  3043.    In REGS, return the indices of the virtual concatenation of STRING1
  3044.    and STRING2 that matched the entire BUFP->buffer and its contained
  3045.    subexpressions.
  3046.    
  3047.    Do not consider matching one past the index STOP in the virtual
  3048.    concatenation of STRING1 and STRING2.
  3049.  
  3050.    We return either the position in the strings at which the match was
  3051.    found, -1 if no match, or -2 if error (such as failure
  3052.    stack overflow).  */
  3053.  
  3054. int
  3055. re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
  3056.      struct re_pattern_buffer *bufp;
  3057.      const char *string1, *string2;
  3058.      int size1, size2;
  3059.      int startpos;
  3060.      int range;
  3061.      struct re_registers *regs;
  3062.      int stop;
  3063. {
  3064.   int val;
  3065.   register char *fastmap = bufp->fastmap;
  3066.   register char *translate = bufp->translate;
  3067.   int total_size = size1 + size2;
  3068.   int endpos = startpos + range;
  3069.  
  3070.   /* Check for out-of-range STARTPOS.  */
  3071.   if (startpos < 0 || startpos > total_size)
  3072.     return -1;
  3073.     
  3074.   /* Fix up RANGE if it might eventually take us outside
  3075.      the virtual concatenation of STRING1 and STRING2.  */
  3076.   if (endpos < -1)
  3077.     range = -1 - startpos;
  3078.   else if (endpos > total_size)
  3079.     range = total_size - startpos;
  3080.  
  3081.   /* If the search isn't to be a backwards one, don't waste time in a
  3082.      search for a pattern that must be anchored.  */
  3083.   if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
  3084.     {
  3085.       if (startpos > 0)
  3086.     return -1;
  3087.       else
  3088.     range = 1;
  3089.     }
  3090.  
  3091.   /* Update the fastmap now if not correct already.  */
  3092.   if (fastmap && !bufp->fastmap_accurate)
  3093.     if (re_compile_fastmap (bufp) == -2)
  3094.       return -2;
  3095.   
  3096.   /* Loop through the string, looking for a place to start matching.  */
  3097.   for (;;)
  3098.     { 
  3099.       /* If a fastmap is supplied, skip quickly over characters that
  3100.          cannot be the start of a match.  If the pattern can match the
  3101.          null string, however, we don't need to skip characters; we want
  3102.          the first null string.  */
  3103.       if (fastmap && startpos < total_size && !bufp->can_be_null)
  3104.     {
  3105.       if (range > 0)    /* Searching forwards.  */
  3106.         {
  3107.           register const char *d;
  3108.           register int lim = 0;
  3109.           int irange = range;
  3110.  
  3111.               if (startpos < size1 && startpos + range >= size1)
  3112.                 lim = range - (size1 - startpos);
  3113.  
  3114.           d = (startpos >= size1 ? string2 - size1 : string1) + startpos;
  3115.    
  3116.               /* Written out as an if-else to avoid testing `translate'
  3117.                  inside the loop.  */
  3118.           if (translate)
  3119.                 while (range > lim
  3120.                        && !fastmap[(unsigned char)
  3121.                    translate[(unsigned char) *d++]])
  3122.                   range--;
  3123.           else
  3124.                 while (range > lim && !fastmap[(unsigned char) *d++])
  3125.                   range--;
  3126.  
  3127.           startpos += irange - range;
  3128.         }
  3129.       else                /* Searching backwards.  */
  3130.         {
  3131.           register char c = (size1 == 0 || startpos >= size1
  3132.                                  ? string2[startpos - size1] 
  3133.                                  : string1[startpos]);
  3134.  
  3135.           if (!fastmap[(unsigned char) TRANSLATE (c)])
  3136.         goto advance;
  3137.         }
  3138.     }
  3139.  
  3140.       /* If can't match the null string, and that's all we have left, fail.  */
  3141.       if (range >= 0 && startpos == total_size && fastmap
  3142.           && !bufp->can_be_null)
  3143.     return -1;
  3144.  
  3145.       val = re_match_2 (bufp, string1, size1, string2, size2,
  3146.                     startpos, regs, stop);
  3147.       if (val >= 0)
  3148.     return startpos;
  3149.         
  3150.       if (val == -2)
  3151.     return -2;
  3152.  
  3153.     advance:
  3154.       if (!range) 
  3155.         break;
  3156.       else if (range > 0) 
  3157.         {
  3158.           range--; 
  3159.           startpos++;
  3160.         }
  3161.       else
  3162.         {
  3163.           range++; 
  3164.           startpos--;
  3165.         }
  3166.     }
  3167.   return -1;
  3168. } /* re_search_2 */
  3169.  
  3170. /* Declarations and macros for re_match_2.  */
  3171.  
  3172. static int bcmp_translate ();
  3173. static boolean alt_match_null_string_p (),
  3174.                common_op_match_null_string_p (),
  3175.                group_match_null_string_p ();
  3176.  
  3177. /* This converts PTR, a pointer into one of the search strings `string1'
  3178.    and `string2' into an offset from the beginning of that string.  */
  3179. #define POINTER_TO_OFFSET(ptr)                        \
  3180.   (FIRST_STRING_P (ptr) ? (ptr) - string1 : (ptr) - string2 + size1)
  3181.  
  3182. /* Macros for dealing with the split strings in re_match_2.  */
  3183.  
  3184. #define MATCHING_IN_FIRST_STRING  (dend == end_match_1)
  3185.  
  3186. /* Call before fetching a character with *d.  This switches over to
  3187.    string2 if necessary.  */
  3188. #define PREFETCH()                            \
  3189.   while (d == dend)                                \
  3190.     {                                    \
  3191.       /* End of string2 => fail.  */                    \
  3192.       if (dend == end_match_2)                         \
  3193.         goto fail;                            \
  3194.       /* End of string1 => advance to string2.  */             \
  3195.       d = string2;                                \
  3196.       dend = end_match_2;                        \
  3197.     }
  3198.  
  3199.  
  3200. /* Test if at very beginning or at very end of the virtual concatenation
  3201.    of `string1' and `string2'.  If only one string, it's `string2'.  */
  3202. #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
  3203. #define AT_STRINGS_END(d) ((d) == end2)    
  3204.  
  3205.  
  3206. /* Test if D points to a character which is word-constituent.  We have
  3207.    two special cases to check for: if past the end of string1, look at
  3208.    the first character in string2; and if before the beginning of
  3209.    string2, look at the last character in string1.  */
  3210. #define WORDCHAR_P(d)                            \
  3211.   (SYNTAX ((d) == end1 ? *string2                    \
  3212.            : (d) == string2 - 1 ? *(end1 - 1) : *(d))            \
  3213.    == Sword)
  3214.  
  3215. /* Test if the character before D and the one at D differ with respect
  3216.    to being word-constituent.  */
  3217. #define AT_WORD_BOUNDARY(d)                        \
  3218.   (AT_STRINGS_BEG (d) || AT_STRINGS_END (d)                \
  3219.    || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
  3220.  
  3221.  
  3222. /* Free everything we malloc.  */
  3223. #ifdef MATCH_MAY_ALLOCATE
  3224. #ifdef REGEX_MALLOC
  3225. #define FREE_VAR(var) if (var) free (var); var = NULL
  3226. #define FREE_VARIABLES()                        \
  3227.   do {                                    \
  3228.     FREE_VAR (fail_stack.stack);                    \
  3229.     FREE_VAR (regstart);                        \
  3230.     FREE_VAR (regend);                            \
  3231.     FREE_VAR (old_regstart);                        \
  3232.     FREE_VAR (old_regend);                        \
  3233.     FREE_VAR (best_regstart);                        \
  3234.     FREE_VAR (best_regend);                        \
  3235.     FREE_VAR (reg_info);                        \
  3236.     FREE_VAR (reg_dummy);                        \
  3237.     FREE_VAR (reg_info_dummy);                        \
  3238.   } while (0)
  3239. #else /* not REGEX_MALLOC */
  3240. /* Some MIPS systems (at least) want this to free alloca'd storage.  */
  3241. #define FREE_VARIABLES() alloca (0)
  3242. #endif /* not REGEX_MALLOC */
  3243. #else
  3244. #define FREE_VARIABLES() /* Do nothing!  */
  3245. #endif /* not MATCH_MAY_ALLOCATE */
  3246.  
  3247. /* These values must meet several constraints.  They must not be valid
  3248.    register values; since we have a limit of 255 registers (because
  3249.    we use only one byte in the pattern for the register number), we can
  3250.    use numbers larger than 255.  They must differ by 1, because of
  3251.    NUM_FAILURE_ITEMS above.  And the value for the lowest register must
  3252.    be larger than the value for the highest register, so we do not try
  3253.    to actually save any registers when none are active.  */
  3254. #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
  3255. #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
  3256.  
  3257. /* Matching routines.  */
  3258.  
  3259. #ifndef emacs   /* Emacs never uses this.  */
  3260. /* re_match is like re_match_2 except it takes only a single string.  */
  3261.  
  3262. int
  3263. re_match (bufp, string, size, pos, regs)
  3264.      struct re_pattern_buffer *bufp;
  3265.      const char *string;
  3266.      int size, pos;
  3267.      struct re_registers *regs;
  3268.  {
  3269.   return re_match_2 (bufp, NULL, 0, string, size, pos, regs, size); 
  3270. }
  3271. #endif /* not emacs */
  3272.  
  3273.  
  3274. /* re_match_2 matches the compiled pattern in BUFP against the
  3275.    the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
  3276.    and SIZE2, respectively).  We start matching at POS, and stop
  3277.    matching at STOP.
  3278.    
  3279.    If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
  3280.    store offsets for the substring each group matched in REGS.  See the
  3281.    documentation for exactly how many groups we fill.
  3282.  
  3283.    We return -1 if no match, -2 if an internal error (such as the
  3284.    failure stack overflowing).  Otherwise, we return the length of the
  3285.    matched substring.  */
  3286.  
  3287. int
  3288. re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
  3289.      struct re_pattern_buffer *bufp;
  3290.      const char *string1, *string2;
  3291.      int size1, size2;
  3292.      int pos;
  3293.      struct re_registers *regs;
  3294.      int stop;
  3295. {
  3296.   /* General temporaries.  */
  3297.   int mcnt;
  3298.   unsigned char *p1;
  3299.  
  3300.   /* Just past the end of the corresponding string.  */
  3301.   const char *end1, *end2;
  3302.  
  3303.   /* Pointers into string1 and string2, just past the last characters in
  3304.      each to consider matching.  */
  3305.   const char *end_match_1, *end_match_2;
  3306.  
  3307.   /* Where we are in the data, and the end of the current string.  */
  3308.   const char *d, *dend;
  3309.   
  3310.   /* Where we are in the pattern, and the end of the pattern.  */
  3311.   unsigned char *p = bufp->buffer;
  3312.   register unsigned char *pend = p + bufp->used;
  3313.  
  3314.   /* We use this to map every character in the string.  */
  3315.   char *translate = bufp->translate;
  3316.  
  3317.   /* Failure point stack.  Each place that can handle a failure further
  3318.      down the line pushes a failure point on this stack.  It consists of
  3319.      restart, regend, and reg_info for all registers corresponding to
  3320.      the subexpressions we're currently inside, plus the number of such
  3321.      registers, and, finally, two char *'s.  The first char * is where
  3322.      to resume scanning the pattern; the second one is where to resume
  3323.      scanning the strings.  If the latter is zero, the failure point is
  3324.      a ``dummy''; if a failure happens and the failure point is a dummy,
  3325.      it gets discarded and the next next one is tried.  */
  3326. #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global.  */
  3327.   fail_stack_type fail_stack;
  3328. #endif
  3329. #ifdef DEBUG
  3330.   static unsigned failure_id = 0;
  3331.   unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
  3332. #endif
  3333.  
  3334.   /* We fill all the registers internally, independent of what we
  3335.      return, for use in backreferences.  The number here includes
  3336.      an element for register zero.  */
  3337.   unsigned num_regs = bufp->re_nsub + 1;
  3338.   
  3339.   /* The currently active registers.  */
  3340.   unsigned lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3341.   unsigned highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3342.  
  3343.   /* Information on the contents of registers. These are pointers into
  3344.      the input strings; they record just what was matched (on this
  3345.      attempt) by a subexpression part of the pattern, that is, the
  3346.      regnum-th regstart pointer points to where in the pattern we began
  3347.      matching and the regnum-th regend points to right after where we
  3348.      stopped matching the regnum-th subexpression.  (The zeroth register
  3349.      keeps track of what the whole pattern matches.)  */
  3350. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global.  */
  3351.   const char **regstart, **regend;
  3352. #endif
  3353.  
  3354.   /* If a group that's operated upon by a repetition operator fails to
  3355.      match anything, then the register for its start will need to be
  3356.      restored because it will have been set to wherever in the string we
  3357.      are when we last see its open-group operator.  Similarly for a
  3358.      register's end.  */
  3359. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global.  */
  3360.   const char **old_regstart, **old_regend;
  3361. #endif
  3362.  
  3363.   /* The is_active field of reg_info helps us keep track of which (possibly
  3364.      nested) subexpressions we are currently in. The matched_something
  3365.      field of reg_info[reg_num] helps us tell whether or not we have
  3366.      matched any of the pattern so far this time through the reg_num-th
  3367.      subexpression.  These two fields get reset each time through any
  3368.      loop their register is in.  */
  3369. #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global.  */
  3370.   register_info_type *reg_info; 
  3371. #endif
  3372.  
  3373.   /* The following record the register info as found in the above
  3374.      variables when we find a match better than any we've seen before. 
  3375.      This happens as we backtrack through the failure points, which in
  3376.      turn happens only if we have not yet matched the entire string. */
  3377.   unsigned best_regs_set = false;
  3378. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global.  */
  3379.   const char **best_regstart, **best_regend;
  3380. #endif
  3381.   
  3382.   /* Logically, this is `best_regend[0]'.  But we don't want to have to
  3383.      allocate space for that if we're not allocating space for anything
  3384.      else (see below).  Also, we never need info about register 0 for
  3385.      any of the other register vectors, and it seems rather a kludge to
  3386.      treat `best_regend' differently than the rest.  So we keep track of
  3387.      the end of the best match so far in a separate variable.  We
  3388.      initialize this to NULL so that when we backtrack the first time
  3389.      and need to test it, it's not garbage.  */
  3390.   const char *match_end = NULL;
  3391.  
  3392.   /* Used when we pop values we don't care about.  */
  3393. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global.  */
  3394.   const char **reg_dummy;
  3395.   register_info_type *reg_info_dummy;
  3396. #endif
  3397.  
  3398. #ifdef DEBUG
  3399.   /* Counts the total number of registers pushed.  */
  3400.   unsigned num_regs_pushed = 0;     
  3401. #endif
  3402.  
  3403.   DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
  3404.   
  3405.   INIT_FAIL_STACK ();
  3406.   
  3407. #ifdef MATCH_MAY_ALLOCATE
  3408.   /* Do not bother to initialize all the register variables if there are
  3409.      no groups in the pattern, as it takes a fair amount of time.  If
  3410.      there are groups, we include space for register 0 (the whole
  3411.      pattern), even though we never use it, since it simplifies the
  3412.      array indexing.  We should fix this.  */
  3413.   if (bufp->re_nsub)
  3414.     {
  3415.       regstart = REGEX_TALLOC (num_regs, const char *);
  3416.       regend = REGEX_TALLOC (num_regs, const char *);
  3417.       old_regstart = REGEX_TALLOC (num_regs, const char *);
  3418.       old_regend = REGEX_TALLOC (num_regs, const char *);
  3419.       best_regstart = REGEX_TALLOC (num_regs, const char *);
  3420.       best_regend = REGEX_TALLOC (num_regs, const char *);
  3421.       reg_info = REGEX_TALLOC (num_regs, register_info_type);
  3422.       reg_dummy = REGEX_TALLOC (num_regs, const char *);
  3423.       reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type);
  3424.  
  3425.       if (!(regstart && regend && old_regstart && old_regend && reg_info 
  3426.             && best_regstart && best_regend && reg_dummy && reg_info_dummy)) 
  3427.         {
  3428.           FREE_VARIABLES ();
  3429.           return -2;
  3430.         }
  3431.     }
  3432. #if defined (REGEX_MALLOC)
  3433.   else
  3434.     {
  3435.       /* We must initialize all our variables to NULL, so that
  3436.          `FREE_VARIABLES' doesn't try to free them.  */
  3437.       regstart = regend = old_regstart = old_regend = best_regstart
  3438.         = best_regend = reg_dummy = NULL;
  3439.       reg_info = reg_info_dummy = (register_info_type *) NULL;
  3440.     }
  3441. #endif /* REGEX_MALLOC */
  3442. #endif /* MATCH_MAY_ALLOCATE */
  3443.  
  3444.   /* The starting position is bogus.  */
  3445.   if (pos < 0 || pos > size1 + size2)
  3446.     {
  3447.       FREE_VARIABLES ();
  3448.       return -1;
  3449.     }
  3450.     
  3451.   /* Initialize subexpression text positions to -1 to mark ones that no
  3452.      start_memory/stop_memory has been seen for. Also initialize the
  3453.      register information struct.  */
  3454.   for (mcnt = 1; mcnt < num_regs; mcnt++)
  3455.     {
  3456.       regstart[mcnt] = regend[mcnt] 
  3457.         = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
  3458.         
  3459.       REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
  3460.       IS_ACTIVE (reg_info[mcnt]) = 0;
  3461.       MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3462.       EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3463.     }
  3464.   
  3465.   /* We move `string1' into `string2' if the latter's empty -- but not if
  3466.      `string1' is null.  */
  3467.   if (size2 == 0 && string1 != NULL)
  3468.     {
  3469.       string2 = string1;
  3470.       size2 = size1;
  3471.       string1 = 0;
  3472.       size1 = 0;
  3473.     }
  3474.   end1 = string1 + size1;
  3475.   end2 = string2 + size2;
  3476.  
  3477.   /* Compute where to stop matching, within the two strings.  */
  3478.   if (stop <= size1)
  3479.     {
  3480.       end_match_1 = string1 + stop;
  3481.       end_match_2 = string2;
  3482.     }
  3483.   else
  3484.     {
  3485.       end_match_1 = end1;
  3486.       end_match_2 = string2 + stop - size1;
  3487.     }
  3488.  
  3489.   /* `p' scans through the pattern as `d' scans through the data. 
  3490.      `dend' is the end of the input string that `d' points within.  `d'
  3491.      is advanced into the following input string whenever necessary, but
  3492.      this happens before fetching; therefore, at the beginning of the
  3493.      loop, `d' can be pointing at the end of a string, but it cannot
  3494.      equal `string2'.  */
  3495.   if (size1 > 0 && pos <= size1)
  3496.     {
  3497.       d = string1 + pos;
  3498.       dend = end_match_1;
  3499.     }
  3500.   else
  3501.     {
  3502.       d = string2 + pos - size1;
  3503.       dend = end_match_2;
  3504.     }
  3505.  
  3506.   DEBUG_PRINT1 ("The compiled pattern is: ");
  3507.   DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
  3508.   DEBUG_PRINT1 ("The string to match is: `");
  3509.   DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
  3510.   DEBUG_PRINT1 ("'\n");
  3511.   
  3512.   /* This loops over pattern commands.  It exits by returning from the
  3513.      function if the match is complete, or it drops through if the match
  3514.      fails at this starting point in the input data.  */
  3515.   for (;;)
  3516.     {
  3517.       DEBUG_PRINT2 ("\n0x%x: ", p);
  3518.  
  3519.       if (p == pend)
  3520.     { /* End of pattern means we might have succeeded.  */
  3521.           DEBUG_PRINT1 ("end of pattern ... ");
  3522.           
  3523.       /* If we haven't matched the entire string, and we want the
  3524.              longest match, try backtracking.  */
  3525.           if (d != end_match_2)
  3526.         {
  3527.               DEBUG_PRINT1 ("backtracking.\n");
  3528.               
  3529.               if (!FAIL_STACK_EMPTY ())
  3530.                 { /* More failure points to try.  */
  3531.                   boolean same_str_p = (FIRST_STRING_P (match_end) 
  3532.                                 == MATCHING_IN_FIRST_STRING);
  3533.  
  3534.                   /* If exceeds best match so far, save it.  */
  3535.                   if (!best_regs_set
  3536.                       || (same_str_p && d > match_end)
  3537.                       || (!same_str_p && !MATCHING_IN_FIRST_STRING))
  3538.                     {
  3539.                       best_regs_set = true;
  3540.                       match_end = d;
  3541.                       
  3542.                       DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
  3543.                       
  3544.                       for (mcnt = 1; mcnt < num_regs; mcnt++)
  3545.                         {
  3546.                           best_regstart[mcnt] = regstart[mcnt];
  3547.                           best_regend[mcnt] = regend[mcnt];
  3548.                         }
  3549.                     }
  3550.                   goto fail;           
  3551.                 }
  3552.  
  3553.               /* If no failure points, don't restore garbage.  */
  3554.               else if (best_regs_set)   
  3555.                 {
  3556.               restore_best_regs:
  3557.                   /* Restore best match.  It may happen that `dend ==
  3558.                      end_match_1' while the restored d is in string2.
  3559.                      For example, the pattern `x.*y.*z' against the
  3560.                      strings `x-' and `y-z-', if the two strings are
  3561.                      not consecutive in memory.  */
  3562.                   DEBUG_PRINT1 ("Restoring best registers.\n");
  3563.                   
  3564.                   d = match_end;
  3565.                   dend = ((d >= string1 && d <= end1)
  3566.                    ? end_match_1 : end_match_2);
  3567.  
  3568.           for (mcnt = 1; mcnt < num_regs; mcnt++)
  3569.             {
  3570.               regstart[mcnt] = best_regstart[mcnt];
  3571.               regend[mcnt] = best_regend[mcnt];
  3572.             }
  3573.                 }
  3574.             } /* d != end_match_2 */
  3575.  
  3576.           DEBUG_PRINT1 ("Accepting match.\n");
  3577.  
  3578.           /* If caller wants register contents data back, do it.  */
  3579.           if (regs && !bufp->no_sub)
  3580.         {
  3581.               /* Have the register data arrays been allocated?  */
  3582.               if (bufp->regs_allocated == REGS_UNALLOCATED)
  3583.                 { /* No.  So allocate them with malloc.  We need one
  3584.                      extra element beyond `num_regs' for the `-1' marker
  3585.                      GNU code uses.  */
  3586.                   regs->num_regs = MAX (RE_NREGS, num_regs + 1);
  3587.                   regs->start = TALLOC (regs->num_regs, regoff_t);
  3588.                   regs->end = TALLOC (regs->num_regs, regoff_t);
  3589.                   if (regs->start == NULL || regs->end == NULL)
  3590.                     return -2;
  3591.                   bufp->regs_allocated = REGS_REALLOCATE;
  3592.                 }
  3593.               else if (bufp->regs_allocated == REGS_REALLOCATE)
  3594.                 { /* Yes.  If we need more elements than were already
  3595.                      allocated, reallocate them.  If we need fewer, just
  3596.                      leave it alone.  */
  3597.                   if (regs->num_regs < num_regs + 1)
  3598.                     {
  3599.                       regs->num_regs = num_regs + 1;
  3600.                       RETALLOC (regs->start, regs->num_regs, regoff_t);
  3601.                       RETALLOC (regs->end, regs->num_regs, regoff_t);
  3602.                       if (regs->start == NULL || regs->end == NULL)
  3603.                         return -2;
  3604.                     }
  3605.                 }
  3606.               else
  3607.         {
  3608.           /* These braces fend off a "empty body in an else-statement"
  3609.              warning under GCC when assert expands to nothing.  */
  3610.           assert (bufp->regs_allocated == REGS_FIXED);
  3611.         }
  3612.  
  3613.               /* Convert the pointer data in `regstart' and `regend' to
  3614.                  indices.  Register zero has to be set differently,
  3615.                  since we haven't kept track of any info for it.  */
  3616.               if (regs->num_regs > 0)
  3617.                 {
  3618.                   regs->start[0] = pos;
  3619.                   regs->end[0] = (MATCHING_IN_FIRST_STRING ? d - string1
  3620.                       : d - string2 + size1);
  3621.                 }
  3622.               
  3623.               /* Go through the first `min (num_regs, regs->num_regs)'
  3624.                  registers, since that is all we initialized.  */
  3625.           for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
  3626.         {
  3627.                   if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
  3628.                     regs->start[mcnt] = regs->end[mcnt] = -1;
  3629.                   else
  3630.                     {
  3631.               regs->start[mcnt] = POINTER_TO_OFFSET (regstart[mcnt]);
  3632.                       regs->end[mcnt] = POINTER_TO_OFFSET (regend[mcnt]);
  3633.                     }
  3634.         }
  3635.               
  3636.               /* If the regs structure we return has more elements than
  3637.                  were in the pattern, set the extra elements to -1.  If
  3638.                  we (re)allocated the registers, this is the case,
  3639.                  because we always allocate enough to have at least one
  3640.                  -1 at the end.  */
  3641.               for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
  3642.                 regs->start[mcnt] = regs->end[mcnt] = -1;
  3643.         } /* regs && !bufp->no_sub */
  3644.  
  3645.           FREE_VARIABLES ();
  3646.           DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
  3647.                         nfailure_points_pushed, nfailure_points_popped,
  3648.                         nfailure_points_pushed - nfailure_points_popped);
  3649.           DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
  3650.  
  3651.           mcnt = d - pos - (MATCHING_IN_FIRST_STRING 
  3652.                 ? string1 
  3653.                 : string2 - size1);
  3654.  
  3655.           DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
  3656.  
  3657.           return mcnt;
  3658.         }
  3659.  
  3660.       /* Otherwise match next pattern command.  */
  3661. #ifdef SWITCH_ENUM_BUG
  3662.       switch ((int) ((re_opcode_t) *p++))
  3663. #else
  3664.       switch ((re_opcode_t) *p++)
  3665. #endif
  3666.     {
  3667.         /* Ignore these.  Used to ignore the n of succeed_n's which
  3668.            currently have n == 0.  */
  3669.         case no_op:
  3670.           DEBUG_PRINT1 ("EXECUTING no_op.\n");
  3671.           break;
  3672.  
  3673.  
  3674.         /* Match the next n pattern characters exactly.  The following
  3675.            byte in the pattern defines n, and the n bytes after that
  3676.            are the characters to match.  */
  3677.     case exactn:
  3678.       mcnt = *p++;
  3679.           DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
  3680.  
  3681.           /* This is written out as an if-else so we don't waste time
  3682.              testing `translate' inside the loop.  */
  3683.           if (translate)
  3684.         {
  3685.           do
  3686.         {
  3687.           PREFETCH ();
  3688.           if (translate[(unsigned char) *d++] != (char) *p++)
  3689.                     goto fail;
  3690.         }
  3691.           while (--mcnt);
  3692.         }
  3693.       else
  3694.         {
  3695.           do
  3696.         {
  3697.           PREFETCH ();
  3698.           if (*d++ != (char) *p++) goto fail;
  3699.         }
  3700.           while (--mcnt);
  3701.         }
  3702.       SET_REGS_MATCHED ();
  3703.           break;
  3704.  
  3705.  
  3706.         /* Match any character except possibly a newline or a null.  */
  3707.     case anychar:
  3708.           DEBUG_PRINT1 ("EXECUTING anychar.\n");
  3709.  
  3710.           PREFETCH ();
  3711.  
  3712.           if ((!(bufp->syntax & RE_DOT_NEWLINE) && TRANSLATE (*d) == '\n')
  3713.               || (bufp->syntax & RE_DOT_NOT_NULL && TRANSLATE (*d) == '\000'))
  3714.         goto fail;
  3715.  
  3716.           SET_REGS_MATCHED ();
  3717.           DEBUG_PRINT2 ("  Matched `%d'.\n", *d);
  3718.           d++;
  3719.       break;
  3720.  
  3721.  
  3722.     case charset:
  3723.     case charset_not:
  3724.       {
  3725.         register unsigned char c;
  3726.         boolean not = (re_opcode_t) *(p - 1) == charset_not;
  3727.  
  3728.             DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
  3729.  
  3730.         PREFETCH ();
  3731.         c = TRANSLATE (*d); /* The character to match.  */
  3732.  
  3733.             /* Cast to `unsigned' instead of `unsigned char' in case the
  3734.                bit list is a full 32 bytes long.  */
  3735.         if (c < (unsigned) (*p * BYTEWIDTH)
  3736.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  3737.           not = !not;
  3738.  
  3739.         p += 1 + *p;
  3740.  
  3741.         if (!not) goto fail;
  3742.             
  3743.         SET_REGS_MATCHED ();
  3744.             d++;
  3745.         break;
  3746.       }
  3747.  
  3748.  
  3749.         /* The beginning of a group is represented by start_memory.
  3750.            The arguments are the register number in the next byte, and the
  3751.            number of groups inner to this one in the next.  The text
  3752.            matched within the group is recorded (in the internal
  3753.            registers data structure) under the register number.  */
  3754.         case start_memory:
  3755.       DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]);
  3756.  
  3757.           /* Find out if this group can match the empty string.  */
  3758.       p1 = p;        /* To send to group_match_null_string_p.  */
  3759.           
  3760.           if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
  3761.             REG_MATCH_NULL_STRING_P (reg_info[*p]) 
  3762.               = group_match_null_string_p (&p1, pend, reg_info);
  3763.  
  3764.           /* Save the position in the string where we were the last time
  3765.              we were at this open-group operator in case the group is
  3766.              operated upon by a repetition operator, e.g., with `(a*)*b'
  3767.              against `ab'; then we want to ignore where we are now in
  3768.              the string in case this attempt to match fails.  */
  3769.           old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  3770.                              ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
  3771.                              : regstart[*p];
  3772.       DEBUG_PRINT2 ("  old_regstart: %d\n", 
  3773.              POINTER_TO_OFFSET (old_regstart[*p]));
  3774.  
  3775.           regstart[*p] = d;
  3776.       DEBUG_PRINT2 ("  regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
  3777.  
  3778.           IS_ACTIVE (reg_info[*p]) = 1;
  3779.           MATCHED_SOMETHING (reg_info[*p]) = 0;
  3780.           
  3781.           /* This is the new highest active register.  */
  3782.           highest_active_reg = *p;
  3783.           
  3784.           /* If nothing was active before, this is the new lowest active
  3785.              register.  */
  3786.           if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  3787.             lowest_active_reg = *p;
  3788.  
  3789.           /* Move past the register number and inner group count.  */
  3790.           p += 2;
  3791.           break;
  3792.  
  3793.  
  3794.         /* The stop_memory opcode represents the end of a group.  Its
  3795.            arguments are the same as start_memory's: the register
  3796.            number, and the number of inner groups.  */
  3797.     case stop_memory:
  3798.       DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]);
  3799.              
  3800.           /* We need to save the string position the last time we were at
  3801.              this close-group operator in case the group is operated
  3802.              upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
  3803.              against `aba'; then we want to ignore where we are now in
  3804.              the string in case this attempt to match fails.  */
  3805.           old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  3806.                            ? REG_UNSET (regend[*p]) ? d : regend[*p]
  3807.                : regend[*p];
  3808.       DEBUG_PRINT2 ("      old_regend: %d\n", 
  3809.              POINTER_TO_OFFSET (old_regend[*p]));
  3810.  
  3811.           regend[*p] = d;
  3812.       DEBUG_PRINT2 ("      regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
  3813.  
  3814.           /* This register isn't active anymore.  */
  3815.           IS_ACTIVE (reg_info[*p]) = 0;
  3816.           
  3817.           /* If this was the only register active, nothing is active
  3818.              anymore.  */
  3819.           if (lowest_active_reg == highest_active_reg)
  3820.             {
  3821.               lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3822.               highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3823.             }
  3824.           else
  3825.             { /* We must scan for the new highest active register, since
  3826.                  it isn't necessarily one less than now: consider
  3827.                  (a(b)c(d(e)f)g).  When group 3 ends, after the f), the
  3828.                  new highest active register is 1.  */
  3829.               unsigned char r = *p - 1;
  3830.               while (r > 0 && !IS_ACTIVE (reg_info[r]))
  3831.                 r--;
  3832.               
  3833.               /* If we end up at register zero, that means that we saved
  3834.                  the registers as the result of an `on_failure_jump', not
  3835.                  a `start_memory', and we jumped to past the innermost
  3836.                  `stop_memory'.  For example, in ((.)*) we save
  3837.                  registers 1 and 2 as a result of the *, but when we pop
  3838.                  back to the second ), we are at the stop_memory 1.
  3839.                  Thus, nothing is active.  */
  3840.           if (r == 0)
  3841.                 {
  3842.                   lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3843.                   highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3844.                 }
  3845.               else
  3846.                 highest_active_reg = r;
  3847.             }
  3848.           
  3849.           /* If just failed to match something this time around with a
  3850.              group that's operated on by a repetition operator, try to
  3851.              force exit from the ``loop'', and restore the register
  3852.              information for this group that we had before trying this
  3853.              last match.  */
  3854.           if ((!MATCHED_SOMETHING (reg_info[*p])
  3855.                || (re_opcode_t) p[-3] == start_memory)
  3856.           && (p + 2) < pend)              
  3857.             {
  3858.               boolean is_a_jump_n = false;
  3859.               
  3860.               p1 = p + 2;
  3861.               mcnt = 0;
  3862.               switch ((re_opcode_t) *p1++)
  3863.                 {
  3864.                   case jump_n:
  3865.             is_a_jump_n = true;
  3866.                   case pop_failure_jump:
  3867.           case maybe_pop_jump:
  3868.           case jump:
  3869.           case dummy_failure_jump:
  3870.                     EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  3871.             if (is_a_jump_n)
  3872.               p1 += 2;
  3873.                     break;
  3874.                   
  3875.                   default:
  3876.                     /* do nothing */ ;
  3877.                 }
  3878.           p1 += mcnt;
  3879.         
  3880.               /* If the next operation is a jump backwards in the pattern
  3881.              to an on_failure_jump right before the start_memory
  3882.                  corresponding to this stop_memory, exit from the loop
  3883.                  by forcing a failure after pushing on the stack the
  3884.                  on_failure_jump's jump in the pattern, and d.  */
  3885.               if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
  3886.                   && (re_opcode_t) p1[3] == start_memory && p1[4] == *p)
  3887.         {
  3888.                   /* If this group ever matched anything, then restore
  3889.                      what its registers were before trying this last
  3890.                      failed match, e.g., with `(a*)*b' against `ab' for
  3891.                      regstart[1], and, e.g., with `((a*)*(b*)*)*'
  3892.                      against `aba' for regend[3].
  3893.                      
  3894.                      Also restore the registers for inner groups for,
  3895.                      e.g., `((a*)(b*))*' against `aba' (register 3 would
  3896.                      otherwise get trashed).  */
  3897.                      
  3898.                   if (EVER_MATCHED_SOMETHING (reg_info[*p]))
  3899.             {
  3900.               unsigned r; 
  3901.         
  3902.                       EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
  3903.                       
  3904.               /* Restore this and inner groups' (if any) registers.  */
  3905.                       for (r = *p; r < *p + *(p + 1); r++)
  3906.                         {
  3907.                           regstart[r] = old_regstart[r];
  3908.  
  3909.                           /* xx why this test?  */
  3910.                           if ((int) old_regend[r] >= (int) regstart[r])
  3911.                             regend[r] = old_regend[r];
  3912.                         }     
  3913.                     }
  3914.           p1++;
  3915.                   EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  3916.                   PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
  3917.  
  3918.                   goto fail;
  3919.                 }
  3920.             }
  3921.           
  3922.           /* Move past the register number and the inner group count.  */
  3923.           p += 2;
  3924.           break;
  3925.  
  3926.  
  3927.     /* \<digit> has been turned into a `duplicate' command which is
  3928.            followed by the numeric value of <digit> as the register number.  */
  3929.         case duplicate:
  3930.       {
  3931.         register const char *d2, *dend2;
  3932.         int regno = *p++;   /* Get which register to match against.  */
  3933.         DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
  3934.  
  3935.         /* Can't back reference a group which we've never matched.  */
  3936.             if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
  3937.               goto fail;
  3938.               
  3939.             /* Where in input to try to start matching.  */
  3940.             d2 = regstart[regno];
  3941.             
  3942.             /* Where to stop matching; if both the place to start and
  3943.                the place to stop matching are in the same string, then
  3944.                set to the place to stop, otherwise, for now have to use
  3945.                the end of the first string.  */
  3946.  
  3947.             dend2 = ((FIRST_STRING_P (regstart[regno]) 
  3948.               == FIRST_STRING_P (regend[regno]))
  3949.              ? regend[regno] : end_match_1);
  3950.         for (;;)
  3951.           {
  3952.         /* If necessary, advance to next segment in register
  3953.                    contents.  */
  3954.         while (d2 == dend2)
  3955.           {
  3956.             if (dend2 == end_match_2) break;
  3957.             if (dend2 == regend[regno]) break;
  3958.  
  3959.                     /* End of string1 => advance to string2. */
  3960.                     d2 = string2;
  3961.                     dend2 = regend[regno];
  3962.           }
  3963.         /* At end of register contents => success */
  3964.         if (d2 == dend2) break;
  3965.  
  3966.         /* If necessary, advance to next segment in data.  */
  3967.         PREFETCH ();
  3968.  
  3969.         /* How many characters left in this segment to match.  */
  3970.         mcnt = dend - d;
  3971.                 
  3972.         /* Want how many consecutive characters we can match in
  3973.                    one shot, so, if necessary, adjust the count.  */
  3974.                 if (mcnt > dend2 - d2)
  3975.           mcnt = dend2 - d2;
  3976.                   
  3977.         /* Compare that many; failure if mismatch, else move
  3978.                    past them.  */
  3979.         if (translate 
  3980.                     ? bcmp_translate (d, d2, mcnt, translate) 
  3981.                     : bcmp (d, d2, mcnt))
  3982.           goto fail;
  3983.         d += mcnt, d2 += mcnt;
  3984.           }
  3985.       }
  3986.       break;
  3987.  
  3988.  
  3989.         /* begline matches the empty string at the beginning of the string
  3990.            (unless `not_bol' is set in `bufp'), and, if
  3991.            `newline_anchor' is set, after newlines.  */
  3992.     case begline:
  3993.           DEBUG_PRINT1 ("EXECUTING begline.\n");
  3994.           
  3995.           if (AT_STRINGS_BEG (d))
  3996.             {
  3997.               if (!bufp->not_bol) break;
  3998.             }
  3999.           else if (d[-1] == '\n' && bufp->newline_anchor)
  4000.             {
  4001.               break;
  4002.             }
  4003.           /* In all other cases, we fail.  */
  4004.           goto fail;
  4005.  
  4006.  
  4007.         /* endline is the dual of begline.  */
  4008.     case endline:
  4009.           DEBUG_PRINT1 ("EXECUTING endline.\n");
  4010.  
  4011.           if (AT_STRINGS_END (d))
  4012.             {
  4013.               if (!bufp->not_eol) break;
  4014.             }
  4015.           
  4016.           /* We have to ``prefetch'' the next character.  */
  4017.           else if ((d == end1 ? *string2 : *d) == '\n'
  4018.                    && bufp->newline_anchor)
  4019.             {
  4020.               break;
  4021.             }
  4022.           goto fail;
  4023.  
  4024.  
  4025.     /* Match at the very beginning of the data.  */
  4026.         case begbuf:
  4027.           DEBUG_PRINT1 ("EXECUTING begbuf.\n");
  4028.           if (AT_STRINGS_BEG (d))
  4029.             break;
  4030.           goto fail;
  4031.  
  4032.  
  4033.     /* Match at the very end of the data.  */
  4034.         case endbuf:
  4035.           DEBUG_PRINT1 ("EXECUTING endbuf.\n");
  4036.       if (AT_STRINGS_END (d))
  4037.         break;
  4038.           goto fail;
  4039.  
  4040.  
  4041.         /* on_failure_keep_string_jump is used to optimize `.*\n'.  It
  4042.            pushes NULL as the value for the string on the stack.  Then
  4043.            `pop_failure_point' will keep the current value for the
  4044.            string, instead of restoring it.  To see why, consider
  4045.            matching `foo\nbar' against `.*\n'.  The .* matches the foo;
  4046.            then the . fails against the \n.  But the next thing we want
  4047.            to do is match the \n against the \n; if we restored the
  4048.            string value, we would be back at the foo.
  4049.            
  4050.            Because this is used only in specific cases, we don't need to
  4051.            check all the things that `on_failure_jump' does, to make
  4052.            sure the right things get saved on the stack.  Hence we don't
  4053.            share its code.  The only reason to push anything on the
  4054.            stack at all is that otherwise we would have to change
  4055.            `anychar's code to do something besides goto fail in this
  4056.            case; that seems worse than this.  */
  4057.         case on_failure_keep_string_jump:
  4058.           DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
  4059.           
  4060.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4061.           DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt);
  4062.  
  4063.           PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
  4064.           break;
  4065.  
  4066.  
  4067.     /* Uses of on_failure_jump:
  4068.         
  4069.            Each alternative starts with an on_failure_jump that points
  4070.            to the beginning of the next alternative.  Each alternative
  4071.            except the last ends with a jump that in effect jumps past
  4072.            the rest of the alternatives.  (They really jump to the
  4073.            ending jump of the following alternative, because tensioning
  4074.            these jumps is a hassle.)
  4075.  
  4076.            Repeats start with an on_failure_jump that points past both
  4077.            the repetition text and either the following jump or
  4078.            pop_failure_jump back to this on_failure_jump.  */
  4079.     case on_failure_jump:
  4080.         on_failure:
  4081.           DEBUG_PRINT1 ("EXECUTING on_failure_jump");
  4082.  
  4083.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4084.           DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt);
  4085.  
  4086.           /* If this on_failure_jump comes right before a group (i.e.,
  4087.              the original * applied to a group), save the information
  4088.              for that group and all inner ones, so that if we fail back
  4089.              to this point, the group's information will be correct.
  4090.              For example, in \(a*\)*\1, we need the preceding group,
  4091.              and in \(\(a*\)b*\)\2, we need the inner group.  */
  4092.  
  4093.           /* We can't use `p' to check ahead because we push
  4094.              a failure point to `p + mcnt' after we do this.  */
  4095.           p1 = p;
  4096.  
  4097.           /* We need to skip no_op's before we look for the
  4098.              start_memory in case this on_failure_jump is happening as
  4099.              the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
  4100.              against aba.  */
  4101.           while (p1 < pend && (re_opcode_t) *p1 == no_op)
  4102.             p1++;
  4103.  
  4104.           if (p1 < pend && (re_opcode_t) *p1 == start_memory)
  4105.             {
  4106.               /* We have a new highest active register now.  This will
  4107.                  get reset at the start_memory we are about to get to,
  4108.                  but we will have saved all the registers relevant to
  4109.                  this repetition op, as described above.  */
  4110.               highest_active_reg = *(p1 + 1) + *(p1 + 2);
  4111.               if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  4112.                 lowest_active_reg = *(p1 + 1);
  4113.             }
  4114.  
  4115.           DEBUG_PRINT1 (":\n");
  4116.           PUSH_FAILURE_POINT (p + mcnt, d, -2);
  4117.           break;
  4118.  
  4119.  
  4120.         /* A smart repeat ends with `maybe_pop_jump'.
  4121.        We change it to either `pop_failure_jump' or `jump'.  */
  4122.         case maybe_pop_jump:
  4123.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4124.           DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
  4125.           {
  4126.         register unsigned char *p2 = p;
  4127.  
  4128.             /* Compare the beginning of the repeat with what in the
  4129.                pattern follows its end. If we can establish that there
  4130.                is nothing that they would both match, i.e., that we
  4131.                would have to backtrack because of (as in, e.g., `a*a')
  4132.                then we can change to pop_failure_jump, because we'll
  4133.                never have to backtrack.
  4134.                
  4135.                This is not true in the case of alternatives: in
  4136.                `(a|ab)*' we do need to backtrack to the `ab' alternative
  4137.                (e.g., if the string was `ab').  But instead of trying to
  4138.                detect that here, the alternative has put on a dummy
  4139.                failure point which is what we will end up popping.  */
  4140.  
  4141.         /* Skip over open/close-group commands.
  4142.            If what follows this loop is a ...+ construct,
  4143.            look at what begins its body, since we will have to
  4144.            match at least one of that.  */
  4145.         while (1)
  4146.           {
  4147.         if (p2 + 2 < pend
  4148.             && ((re_opcode_t) *p2 == stop_memory
  4149.             || (re_opcode_t) *p2 == start_memory))
  4150.           p2 += 3;
  4151.         else if (p2 + 6 < pend
  4152.              && (re_opcode_t) *p2 == dummy_failure_jump)
  4153.           p2 += 6;
  4154.         else
  4155.           break;
  4156.           }
  4157.  
  4158.         p1 = p + mcnt;
  4159.         /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
  4160.            to the `maybe_finalize_jump' of this case.  Examine what 
  4161.            follows.  */
  4162.  
  4163.             /* If we're at the end of the pattern, we can change.  */
  4164.             if (p2 == pend)
  4165.           {
  4166.         /* Consider what happens when matching ":\(.*\)"
  4167.            against ":/".  I don't really understand this code
  4168.            yet.  */
  4169.               p[-3] = (unsigned char) pop_failure_jump;
  4170.                 DEBUG_PRINT1
  4171.                   ("  End of pattern: change to `pop_failure_jump'.\n");
  4172.               }
  4173.  
  4174.             else if ((re_opcode_t) *p2 == exactn
  4175.              || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
  4176.           {
  4177.         register unsigned char c
  4178.                   = *p2 == (unsigned char) endline ? '\n' : p2[2];
  4179.  
  4180.                 if ((re_opcode_t) p1[3] == exactn && p1[5] != c)
  4181.                   {
  4182.               p[-3] = (unsigned char) pop_failure_jump;
  4183.                     DEBUG_PRINT3 ("  %c != %c => pop_failure_jump.\n",
  4184.                                   c, p1[5]);
  4185.                   }
  4186.                   
  4187.         else if ((re_opcode_t) p1[3] == charset
  4188.              || (re_opcode_t) p1[3] == charset_not)
  4189.           {
  4190.             int not = (re_opcode_t) p1[3] == charset_not;
  4191.                     
  4192.             if (c < (unsigned char) (p1[4] * BYTEWIDTH)
  4193.             && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  4194.               not = !not;
  4195.  
  4196.                     /* `not' is equal to 1 if c would match, which means
  4197.                         that we can't change to pop_failure_jump.  */
  4198.             if (!not)
  4199.                       {
  4200.                   p[-3] = (unsigned char) pop_failure_jump;
  4201.                         DEBUG_PRINT1 ("  No match => pop_failure_jump.\n");
  4202.                       }
  4203.           }
  4204.           }
  4205.             else if ((re_opcode_t) *p2 == charset)
  4206.           {
  4207.         register unsigned char c
  4208.                   = *p2 == (unsigned char) endline ? '\n' : p2[2];
  4209.  
  4210.                 if ((re_opcode_t) p1[3] == exactn
  4211.             && ! (p2[1] * BYTEWIDTH > p1[4]
  4212.               && (p2[1 + p1[4] / BYTEWIDTH]
  4213.                   & (1 << (p1[4] % BYTEWIDTH)))))
  4214.                   {
  4215.               p[-3] = (unsigned char) pop_failure_jump;
  4216.                     DEBUG_PRINT3 ("  %c != %c => pop_failure_jump.\n",
  4217.                                   c, p1[5]);
  4218.                   }
  4219.                   
  4220.         else if ((re_opcode_t) p1[3] == charset_not)
  4221.           {
  4222.             int idx;
  4223.             /* We win if the charset_not inside the loop
  4224.                lists every character listed in the charset after.  */
  4225.             for (idx = 0; idx < p2[1]; idx++)
  4226.               if (! (p2[2 + idx] == 0
  4227.                  || (idx < p1[4]
  4228.                  && ((p2[2 + idx] & ~ p1[5 + idx]) == 0))))
  4229.             break;
  4230.  
  4231.             if (idx == p2[1])
  4232.                       {
  4233.                   p[-3] = (unsigned char) pop_failure_jump;
  4234.                         DEBUG_PRINT1 ("  No match => pop_failure_jump.\n");
  4235.                       }
  4236.           }
  4237.         else if ((re_opcode_t) p1[3] == charset)
  4238.           {
  4239.             int idx;
  4240.             /* We win if the charset inside the loop
  4241.                has no overlap with the one after the loop.  */
  4242.             for (idx = 0; idx < p2[1] && idx < p1[4]; idx++)
  4243.               if ((p2[2 + idx] & p1[5 + idx]) != 0)
  4244.             break;
  4245.  
  4246.             if (idx == p2[1] || idx == p1[4])
  4247.                       {
  4248.                   p[-3] = (unsigned char) pop_failure_jump;
  4249.                         DEBUG_PRINT1 ("  No match => pop_failure_jump.\n");
  4250.                       }
  4251.           }
  4252.           }
  4253.       }
  4254.       p -= 2;        /* Point at relative address again.  */
  4255.       if ((re_opcode_t) p[-1] != pop_failure_jump)
  4256.         {
  4257.           p[-1] = (unsigned char) jump;
  4258.               DEBUG_PRINT1 ("  Match => jump.\n");
  4259.           goto unconditional_jump;
  4260.         }
  4261.         /* Note fall through.  */
  4262.  
  4263.  
  4264.     /* The end of a simple repeat has a pop_failure_jump back to
  4265.            its matching on_failure_jump, where the latter will push a
  4266.            failure point.  The pop_failure_jump takes off failure
  4267.            points put on by this pop_failure_jump's matching
  4268.            on_failure_jump; we got through the pattern to here from the
  4269.            matching on_failure_jump, so didn't fail.  */
  4270.         case pop_failure_jump:
  4271.           {
  4272.             /* We need to pass separate storage for the lowest and
  4273.                highest registers, even though we don't care about the
  4274.                actual values.  Otherwise, we will restore only one
  4275.                register from the stack, since lowest will == highest in
  4276.                `pop_failure_point'.  */
  4277.             unsigned dummy_low_reg, dummy_high_reg;
  4278.             unsigned char *pdummy;
  4279.             const char *sdummy;
  4280.  
  4281.             DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
  4282.             POP_FAILURE_POINT (sdummy, pdummy,
  4283.                                dummy_low_reg, dummy_high_reg,
  4284.                                reg_dummy, reg_dummy, reg_info_dummy);
  4285.           }
  4286.           /* Note fall through.  */
  4287.  
  4288.           
  4289.         /* Unconditionally jump (without popping any failure points).  */
  4290.         case jump:
  4291.     unconditional_jump:
  4292.       EXTRACT_NUMBER_AND_INCR (mcnt, p);    /* Get the amount to jump.  */
  4293.           DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
  4294.       p += mcnt;                /* Do the jump.  */
  4295.           DEBUG_PRINT2 ("(to 0x%x).\n", p);
  4296.       break;
  4297.  
  4298.     
  4299.         /* We need this opcode so we can detect where alternatives end
  4300.            in `group_match_null_string_p' et al.  */
  4301.         case jump_past_alt:
  4302.           DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
  4303.           goto unconditional_jump;
  4304.  
  4305.  
  4306.         /* Normally, the on_failure_jump pushes a failure point, which
  4307.            then gets popped at pop_failure_jump.  We will end up at
  4308.            pop_failure_jump, also, and with a pattern of, say, `a+', we
  4309.            are skipping over the on_failure_jump, so we have to push
  4310.            something meaningless for pop_failure_jump to pop.  */
  4311.         case dummy_failure_jump:
  4312.           DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
  4313.           /* It doesn't matter what we push for the string here.  What
  4314.              the code at `fail' tests is the value for the pattern.  */
  4315.           PUSH_FAILURE_POINT (0, 0, -2);
  4316.           goto unconditional_jump;
  4317.  
  4318.  
  4319.         /* At the end of an alternative, we need to push a dummy failure
  4320.            point in case we are followed by a `pop_failure_jump', because
  4321.            we don't want the failure point for the alternative to be
  4322.            popped.  For example, matching `(a|ab)*' against `aab'
  4323.            requires that we match the `ab' alternative.  */
  4324.         case push_dummy_failure:
  4325.           DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
  4326.           /* See comments just above at `dummy_failure_jump' about the
  4327.              two zeroes.  */
  4328.           PUSH_FAILURE_POINT (0, 0, -2);
  4329.           break;
  4330.  
  4331.         /* Have to succeed matching what follows at least n times.
  4332.            After that, handle like `on_failure_jump'.  */
  4333.         case succeed_n: 
  4334.           EXTRACT_NUMBER (mcnt, p + 2);
  4335.           DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
  4336.  
  4337.           assert (mcnt >= 0);
  4338.           /* Originally, this is how many times we HAVE to succeed.  */
  4339.           if (mcnt > 0)
  4340.             {
  4341.                mcnt--;
  4342.            p += 2;
  4343.                STORE_NUMBER_AND_INCR (p, mcnt);
  4344.                DEBUG_PRINT3 ("  Setting 0x%x to %d.\n", p, mcnt);
  4345.             }
  4346.       else if (mcnt == 0)
  4347.             {
  4348.               DEBUG_PRINT2 ("  Setting two bytes from 0x%x to no_op.\n", p+2);
  4349.           p[2] = (unsigned char) no_op;
  4350.               p[3] = (unsigned char) no_op;
  4351.               goto on_failure;
  4352.             }
  4353.           break;
  4354.         
  4355.         case jump_n: 
  4356.           EXTRACT_NUMBER (mcnt, p + 2);
  4357.           DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
  4358.  
  4359.           /* Originally, this is how many times we CAN jump.  */
  4360.           if (mcnt)
  4361.             {
  4362.                mcnt--;
  4363.                STORE_NUMBER (p + 2, mcnt);
  4364.            goto unconditional_jump;         
  4365.             }
  4366.           /* If don't have to jump any more, skip over the rest of command.  */
  4367.       else      
  4368.         p += 4;             
  4369.           break;
  4370.         
  4371.     case set_number_at:
  4372.       {
  4373.             DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
  4374.  
  4375.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4376.             p1 = p + mcnt;
  4377.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4378.             DEBUG_PRINT3 ("  Setting 0x%x to %d.\n", p1, mcnt);
  4379.         STORE_NUMBER (p1, mcnt);
  4380.             break;
  4381.           }
  4382.  
  4383.         case wordbound:
  4384.           DEBUG_PRINT1 ("EXECUTING wordbound.\n");
  4385.           if (AT_WORD_BOUNDARY (d))
  4386.         break;
  4387.           goto fail;
  4388.  
  4389.     case notwordbound:
  4390.           DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
  4391.       if (AT_WORD_BOUNDARY (d))
  4392.         goto fail;
  4393.           break;
  4394.  
  4395.     case wordbeg:
  4396.           DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
  4397.       if (WORDCHAR_P (d) && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1)))
  4398.         break;
  4399.           goto fail;
  4400.  
  4401.     case wordend:
  4402.           DEBUG_PRINT1 ("EXECUTING wordend.\n");
  4403.       if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1)
  4404.               && (!WORDCHAR_P (d) || AT_STRINGS_END (d)))
  4405.         break;
  4406.           goto fail;
  4407.  
  4408. #ifdef emacs
  4409. #ifdef emacs19
  4410.       case before_dot:
  4411.           DEBUG_PRINT1 ("EXECUTING before_dot.\n");
  4412.        if (PTR_CHAR_POS ((unsigned char *) d) >= point)
  4413.           goto fail;
  4414.         break;
  4415.   
  4416.       case at_dot:
  4417.           DEBUG_PRINT1 ("EXECUTING at_dot.\n");
  4418.        if (PTR_CHAR_POS ((unsigned char *) d) != point)
  4419.           goto fail;
  4420.         break;
  4421.   
  4422.       case after_dot:
  4423.           DEBUG_PRINT1 ("EXECUTING after_dot.\n");
  4424.           if (PTR_CHAR_POS ((unsigned char *) d) <= point)
  4425.           goto fail;
  4426.         break;
  4427. #else /* not emacs19 */
  4428.     case at_dot:
  4429.           DEBUG_PRINT1 ("EXECUTING at_dot.\n");
  4430.       if (PTR_CHAR_POS ((unsigned char *) d) + 1 != point)
  4431.         goto fail;
  4432.       break;
  4433. #endif /* not emacs19 */
  4434.  
  4435.     case syntaxspec:
  4436.           DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
  4437.       mcnt = *p++;
  4438.       goto matchsyntax;
  4439.  
  4440.         case wordchar:
  4441.           DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
  4442.       mcnt = (int) Sword;
  4443.         matchsyntax:
  4444.       PREFETCH ();
  4445.       if (SYNTAX (*d++) != (enum syntaxcode) mcnt)
  4446.             goto fail;
  4447.           SET_REGS_MATCHED ();
  4448.       break;
  4449.  
  4450.     case notsyntaxspec:
  4451.           DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
  4452.       mcnt = *p++;
  4453.       goto matchnotsyntax;
  4454.  
  4455.         case notwordchar:
  4456.           DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
  4457.       mcnt = (int) Sword;
  4458.         matchnotsyntax:
  4459.       PREFETCH ();
  4460.       if (SYNTAX (*d++) == (enum syntaxcode) mcnt)
  4461.             goto fail;
  4462.       SET_REGS_MATCHED ();
  4463.           break;
  4464.  
  4465. #else /* not emacs */
  4466.     case wordchar:
  4467.           DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
  4468.       PREFETCH ();
  4469.           if (!WORDCHAR_P (d))
  4470.             goto fail;
  4471.       SET_REGS_MATCHED ();
  4472.           d++;
  4473.       break;
  4474.       
  4475.     case notwordchar:
  4476.           DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
  4477.       PREFETCH ();
  4478.       if (WORDCHAR_P (d))
  4479.             goto fail;
  4480.           SET_REGS_MATCHED ();
  4481.           d++;
  4482.       break;
  4483. #endif /* not emacs */
  4484.           
  4485.         default:
  4486.           abort ();
  4487.     }
  4488.       continue;  /* Successfully executed one pattern command; keep going.  */
  4489.  
  4490.  
  4491.     /* We goto here if a matching operation fails. */
  4492.     fail:
  4493.       if (!FAIL_STACK_EMPTY ())
  4494.     { /* A restart point is known.  Restore to that state.  */
  4495.           DEBUG_PRINT1 ("\nFAIL:\n");
  4496.           POP_FAILURE_POINT (d, p,
  4497.                              lowest_active_reg, highest_active_reg,
  4498.                              regstart, regend, reg_info);
  4499.  
  4500.           /* If this failure point is a dummy, try the next one.  */
  4501.           if (!p)
  4502.         goto fail;
  4503.  
  4504.           /* If we failed to the end of the pattern, don't examine *p.  */
  4505.       assert (p <= pend);
  4506.           if (p < pend)
  4507.             {
  4508.               boolean is_a_jump_n = false;
  4509.               
  4510.               /* If failed to a backwards jump that's part of a repetition
  4511.                  loop, need to pop this failure point and use the next one.  */
  4512.               switch ((re_opcode_t) *p)
  4513.                 {
  4514.                 case jump_n:
  4515.                   is_a_jump_n = true;
  4516.                 case maybe_pop_jump:
  4517.                 case pop_failure_jump:
  4518.                 case jump:
  4519.                   p1 = p + 1;
  4520.                   EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4521.                   p1 += mcnt;    
  4522.  
  4523.                   if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
  4524.                       || (!is_a_jump_n
  4525.                           && (re_opcode_t) *p1 == on_failure_jump))
  4526.                     goto fail;
  4527.                   break;
  4528.                 default:
  4529.                   /* do nothing */ ;
  4530.                 }
  4531.             }
  4532.  
  4533.           if (d >= string1 && d <= end1)
  4534.         dend = end_match_1;
  4535.         }
  4536.       else
  4537.         break;   /* Matching at this starting point really fails.  */
  4538.     } /* for (;;) */
  4539.  
  4540.   if (best_regs_set)
  4541.     goto restore_best_regs;
  4542.  
  4543.   FREE_VARIABLES ();
  4544.  
  4545.   return -1;                     /* Failure to match.  */
  4546. } /* re_match_2 */
  4547.  
  4548. /* Subroutine definitions for re_match_2.  */
  4549.  
  4550.  
  4551. /* We are passed P pointing to a register number after a start_memory.
  4552.    
  4553.    Return true if the pattern up to the corresponding stop_memory can
  4554.    match the empty string, and false otherwise.
  4555.    
  4556.    If we find the matching stop_memory, sets P to point to one past its number.
  4557.    Otherwise, sets P to an undefined byte less than or equal to END.
  4558.  
  4559.    We don't handle duplicates properly (yet).  */
  4560.  
  4561. static boolean
  4562. group_match_null_string_p (p, end, reg_info)
  4563.     unsigned char **p, *end;
  4564.     register_info_type *reg_info;
  4565. {
  4566.   int mcnt;
  4567.   /* Point to after the args to the start_memory.  */
  4568.   unsigned char *p1 = *p + 2;
  4569.   
  4570.   while (p1 < end)
  4571.     {
  4572.       /* Skip over opcodes that can match nothing, and return true or
  4573.      false, as appropriate, when we get to one that can't, or to the
  4574.          matching stop_memory.  */
  4575.       
  4576.       switch ((re_opcode_t) *p1)
  4577.         {
  4578.         /* Could be either a loop or a series of alternatives.  */
  4579.         case on_failure_jump:
  4580.           p1++;
  4581.           EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4582.           
  4583.           /* If the next operation is not a jump backwards in the
  4584.          pattern.  */
  4585.  
  4586.       if (mcnt >= 0)
  4587.         {
  4588.               /* Go through the on_failure_jumps of the alternatives,
  4589.                  seeing if any of the alternatives cannot match nothing.
  4590.                  The last alternative starts with only a jump,
  4591.                  whereas the rest start with on_failure_jump and end
  4592.                  with a jump, e.g., here is the pattern for `a|b|c':
  4593.  
  4594.                  /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
  4595.                  /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
  4596.                  /exactn/1/c                        
  4597.  
  4598.                  So, we have to first go through the first (n-1)
  4599.                  alternatives and then deal with the last one separately.  */
  4600.  
  4601.  
  4602.               /* Deal with the first (n-1) alternatives, which start
  4603.                  with an on_failure_jump (see above) that jumps to right
  4604.                  past a jump_past_alt.  */
  4605.  
  4606.               while ((re_opcode_t) p1[mcnt-3] == jump_past_alt)
  4607.                 {
  4608.                   /* `mcnt' holds how many bytes long the alternative
  4609.                      is, including the ending `jump_past_alt' and
  4610.                      its number.  */
  4611.  
  4612.                   if (!alt_match_null_string_p (p1, p1 + mcnt - 3, 
  4613.                                       reg_info))
  4614.                     return false;
  4615.  
  4616.                   /* Move to right after this alternative, including the
  4617.              jump_past_alt.  */
  4618.                   p1 += mcnt;    
  4619.  
  4620.                   /* Break if it's the beginning of an n-th alternative
  4621.                      that doesn't begin with an on_failure_jump.  */
  4622.                   if ((re_opcode_t) *p1 != on_failure_jump)
  4623.                     break;
  4624.         
  4625.           /* Still have to check that it's not an n-th
  4626.              alternative that starts with an on_failure_jump.  */
  4627.           p1++;
  4628.                   EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4629.                   if ((re_opcode_t) p1[mcnt-3] != jump_past_alt)
  4630.                     {
  4631.               /* Get to the beginning of the n-th alternative.  */
  4632.                       p1 -= 3;
  4633.                       break;
  4634.                     }
  4635.                 }
  4636.  
  4637.               /* Deal with the last alternative: go back and get number
  4638.                  of the `jump_past_alt' just before it.  `mcnt' contains
  4639.                  the length of the alternative.  */
  4640.               EXTRACT_NUMBER (mcnt, p1 - 2);
  4641.  
  4642.               if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info))
  4643.                 return false;
  4644.  
  4645.               p1 += mcnt;    /* Get past the n-th alternative.  */
  4646.             } /* if mcnt > 0 */
  4647.           break;
  4648.  
  4649.           
  4650.         case stop_memory:
  4651.       assert (p1[1] == **p);
  4652.           *p = p1 + 2;
  4653.           return true;
  4654.  
  4655.         
  4656.         default: 
  4657.           if (!common_op_match_null_string_p (&p1, end, reg_info))
  4658.             return false;
  4659.         }
  4660.     } /* while p1 < end */
  4661.  
  4662.   return false;
  4663. } /* group_match_null_string_p */
  4664.  
  4665.  
  4666. /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
  4667.    It expects P to be the first byte of a single alternative and END one
  4668.    byte past the last. The alternative can contain groups.  */
  4669.    
  4670. static boolean
  4671. alt_match_null_string_p (p, end, reg_info)
  4672.     unsigned char *p, *end;
  4673.     register_info_type *reg_info;
  4674. {
  4675.   int mcnt;
  4676.   unsigned char *p1 = p;
  4677.   
  4678.   while (p1 < end)
  4679.     {
  4680.       /* Skip over opcodes that can match nothing, and break when we get 
  4681.          to one that can't.  */
  4682.       
  4683.       switch ((re_opcode_t) *p1)
  4684.         {
  4685.     /* It's a loop.  */
  4686.         case on_failure_jump:
  4687.           p1++;
  4688.           EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4689.           p1 += mcnt;
  4690.           break;
  4691.           
  4692.     default: 
  4693.           if (!common_op_match_null_string_p (&p1, end, reg_info))
  4694.             return false;
  4695.         }
  4696.     }  /* while p1 < end */
  4697.  
  4698.   return true;
  4699. } /* alt_match_null_string_p */
  4700.  
  4701.  
  4702. /* Deals with the ops common to group_match_null_string_p and
  4703.    alt_match_null_string_p.  
  4704.    
  4705.    Sets P to one after the op and its arguments, if any.  */
  4706.  
  4707. static boolean
  4708. common_op_match_null_string_p (p, end, reg_info)
  4709.     unsigned char **p, *end;
  4710.     register_info_type *reg_info;
  4711. {
  4712.   int mcnt;
  4713.   boolean ret;
  4714.   int reg_no;
  4715.   unsigned char *p1 = *p;
  4716.  
  4717.   switch ((re_opcode_t) *p1++)
  4718.     {
  4719.     case no_op:
  4720.     case begline:
  4721.     case endline:
  4722.     case begbuf:
  4723.     case endbuf:
  4724.     case wordbeg:
  4725.     case wordend:
  4726.     case wordbound:
  4727.     case notwordbound:
  4728. #ifdef emacs
  4729.     case before_dot:
  4730.     case at_dot:
  4731.     case after_dot:
  4732. #endif
  4733.       break;
  4734.  
  4735.     case start_memory:
  4736.       reg_no = *p1;
  4737.       assert (reg_no > 0 && reg_no <= MAX_REGNUM);
  4738.       ret = group_match_null_string_p (&p1, end, reg_info);
  4739.       
  4740.       /* Have to set this here in case we're checking a group which
  4741.          contains a group and a back reference to it.  */
  4742.  
  4743.       if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
  4744.         REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
  4745.  
  4746.       if (!ret)
  4747.         return false;
  4748.       break;
  4749.           
  4750.     /* If this is an optimized succeed_n for zero times, make the jump.  */
  4751.     case jump:
  4752.       EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4753.       if (mcnt >= 0)
  4754.         p1 += mcnt;
  4755.       else
  4756.         return false;
  4757.       break;
  4758.  
  4759.     case succeed_n:
  4760.       /* Get to the number of times to succeed.  */
  4761.       p1 += 2;        
  4762.       EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4763.  
  4764.       if (mcnt == 0)
  4765.         {
  4766.           p1 -= 4;
  4767.           EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4768.           p1 += mcnt;
  4769.         }
  4770.       else
  4771.         return false;
  4772.       break;
  4773.  
  4774.     case duplicate: 
  4775.       if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
  4776.         return false;
  4777.       break;
  4778.  
  4779.     case set_number_at:
  4780.       p1 += 4;
  4781.  
  4782.     default:
  4783.       /* All other opcodes mean we cannot match the empty string.  */
  4784.       return false;
  4785.   }
  4786.  
  4787.   *p = p1;
  4788.   return true;
  4789. } /* common_op_match_null_string_p */
  4790.  
  4791.  
  4792. /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
  4793.    bytes; nonzero otherwise.  */
  4794.    
  4795. static int
  4796. bcmp_translate (s1, s2, len, translate)
  4797.      unsigned char *s1, *s2;
  4798.      register int len;
  4799.      char *translate;
  4800. {
  4801.   register unsigned char *p1 = s1, *p2 = s2;
  4802.   while (len)
  4803.     {
  4804.       if (translate[*p1++] != translate[*p2++]) return 1;
  4805.       len--;
  4806.     }
  4807.   return 0;
  4808. }
  4809.  
  4810. /* Entry points for GNU code.  */
  4811.  
  4812. /* re_compile_pattern is the GNU regular expression compiler: it
  4813.    compiles PATTERN (of length SIZE) and puts the result in BUFP.
  4814.    Returns 0 if the pattern was valid, otherwise an error string.
  4815.    
  4816.    Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  4817.    are set in BUFP on entry.
  4818.    
  4819.    We call regex_compile to do the actual compilation.  */
  4820.  
  4821. const char *
  4822. re_compile_pattern (pattern, length, bufp)
  4823.      const char *pattern;
  4824.      int length;
  4825.      struct re_pattern_buffer *bufp;
  4826. {
  4827.   reg_errcode_t ret;
  4828.   
  4829.   /* GNU code is written to assume at least RE_NREGS registers will be set
  4830.      (and at least one extra will be -1).  */
  4831.   bufp->regs_allocated = REGS_UNALLOCATED;
  4832.   
  4833.   /* And GNU code determines whether or not to get register information
  4834.      by passing null for the REGS argument to re_match, etc., not by
  4835.      setting no_sub.  */
  4836.   bufp->no_sub = 0;
  4837.   
  4838.   /* Match anchors at newline.  */
  4839.   bufp->newline_anchor = 1;
  4840.   
  4841.   ret = regex_compile (pattern, length, re_syntax_options, bufp);
  4842.  
  4843.   return re_error_msg[(int) ret];
  4844. }     
  4845.  
  4846. /* Entry points compatible with 4.2 BSD regex library.  We don't define
  4847.    them if this is an Emacs or POSIX compilation.  */
  4848.  
  4849. #if !defined (emacs) && !defined (_POSIX_SOURCE)
  4850.  
  4851. /* BSD has one and only one pattern buffer.  */
  4852. static struct re_pattern_buffer re_comp_buf;
  4853.  
  4854. char *
  4855. re_comp (s)
  4856.     const char *s;
  4857. {
  4858.   reg_errcode_t ret;
  4859.   
  4860.   if (!s)
  4861.     {
  4862.       if (!re_comp_buf.buffer)
  4863.     return "No previous regular expression";
  4864.       return 0;
  4865.     }
  4866.  
  4867.   if (!re_comp_buf.buffer)
  4868.     {
  4869.       re_comp_buf.buffer = (unsigned char *) malloc (200);
  4870.       if (re_comp_buf.buffer == NULL)
  4871.         return "Memory exhausted";
  4872.       re_comp_buf.allocated = 200;
  4873.  
  4874.       re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
  4875.       if (re_comp_buf.fastmap == NULL)
  4876.     return "Memory exhausted";
  4877.     }
  4878.  
  4879.   /* Since `re_exec' always passes NULL for the `regs' argument, we
  4880.      don't need to initialize the pattern buffer fields which affect it.  */
  4881.  
  4882.   /* Match anchors at newlines.  */
  4883.   re_comp_buf.newline_anchor = 1;
  4884.  
  4885.   ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
  4886.   
  4887.   /* Yes, we're discarding `const' here.  */
  4888.   return (char *) re_error_msg[(int) ret];
  4889. }
  4890.  
  4891.  
  4892. int
  4893. re_exec (s)
  4894.     const char *s;
  4895. {
  4896.   const int len = strlen (s);
  4897.   return
  4898.     0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
  4899. }
  4900. #endif /* not emacs and not _POSIX_SOURCE */
  4901.  
  4902. /* POSIX.2 functions.  Don't define these for Emacs.  */
  4903.  
  4904. #ifndef emacs
  4905.  
  4906. /* regcomp takes a regular expression as a string and compiles it.
  4907.  
  4908.    PREG is a regex_t *.  We do not expect any fields to be initialized,
  4909.    since POSIX says we shouldn't.  Thus, we set
  4910.  
  4911.      `buffer' to the compiled pattern;
  4912.      `used' to the length of the compiled pattern;
  4913.      `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  4914.        REG_EXTENDED bit in CFLAGS is set; otherwise, to
  4915.        RE_SYNTAX_POSIX_BASIC;
  4916.      `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  4917.      `fastmap' and `fastmap_accurate' to zero;
  4918.      `re_nsub' to the number of subexpressions in PATTERN.
  4919.  
  4920.    PATTERN is the address of the pattern string.
  4921.  
  4922.    CFLAGS is a series of bits which affect compilation.
  4923.  
  4924.      If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  4925.      use POSIX basic syntax.
  4926.  
  4927.      If REG_NEWLINE is set, then . and [^...] don't match newline.
  4928.      Also, regexec will try a match beginning after every newline.
  4929.  
  4930.      If REG_ICASE is set, then we considers upper- and lowercase
  4931.      versions of letters to be equivalent when matching.
  4932.  
  4933.      If REG_NOSUB is set, then when PREG is passed to regexec, that
  4934.      routine will report only success or failure, and nothing about the
  4935.      registers.
  4936.  
  4937.    It returns 0 if it succeeds, nonzero if it doesn't.  (See regex.h for
  4938.    the return codes and their meanings.)  */
  4939.  
  4940. int
  4941. regcomp (preg, pattern, cflags)
  4942.     regex_t *preg;
  4943.     const char *pattern; 
  4944.     int cflags;
  4945. {
  4946.   reg_errcode_t ret;
  4947.   unsigned syntax
  4948.     = (cflags & REG_EXTENDED) ?
  4949.       RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
  4950.  
  4951.   /* regex_compile will allocate the space for the compiled pattern.  */
  4952.   preg->buffer = 0;
  4953.   preg->allocated = 0;
  4954.   preg->used = 0;
  4955.   
  4956.   /* Don't bother to use a fastmap when searching.  This simplifies the
  4957.      REG_NEWLINE case: if we used a fastmap, we'd have to put all the
  4958.      characters after newlines into the fastmap.  This way, we just try
  4959.      every character.  */
  4960.   preg->fastmap = 0;
  4961.   
  4962.   if (cflags & REG_ICASE)
  4963.     {
  4964.       unsigned i;
  4965.       
  4966.       preg->translate = (char *) malloc (CHAR_SET_SIZE);
  4967.       if (preg->translate == NULL)
  4968.         return (int) REG_ESPACE;
  4969.  
  4970.       /* Map uppercase characters to corresponding lowercase ones.  */
  4971.       for (i = 0; i < CHAR_SET_SIZE; i++)
  4972.         preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
  4973.     }
  4974.   else
  4975.     preg->translate = NULL;
  4976.  
  4977.   /* If REG_NEWLINE is set, newlines are treated differently.  */
  4978.   if (cflags & REG_NEWLINE)
  4979.     { /* REG_NEWLINE implies neither . nor [^...] match newline.  */
  4980.       syntax &= ~RE_DOT_NEWLINE;
  4981.       syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  4982.       /* It also changes the matching behavior.  */
  4983.       preg->newline_anchor = 1;
  4984.     }
  4985.   else
  4986.     preg->newline_anchor = 0;
  4987.  
  4988.   preg->no_sub = !!(cflags & REG_NOSUB);
  4989.  
  4990.   /* POSIX says a null character in the pattern terminates it, so we 
  4991.      can use strlen here in compiling the pattern.  */
  4992.   ret = regex_compile (pattern, strlen (pattern), syntax, preg);
  4993.   
  4994.   /* POSIX doesn't distinguish between an unmatched open-group and an
  4995.      unmatched close-group: both are REG_EPAREN.  */
  4996.   if (ret == REG_ERPAREN) ret = REG_EPAREN;
  4997.   
  4998.   return (int) ret;
  4999. }
  5000.  
  5001.  
  5002. /* regexec searches for a given pattern, specified by PREG, in the
  5003.    string STRING.
  5004.    
  5005.    If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  5006.    `regcomp', we ignore PMATCH.  Otherwise, we assume PMATCH has at
  5007.    least NMATCH elements, and we set them to the offsets of the
  5008.    corresponding matched substrings.
  5009.    
  5010.    EFLAGS specifies `execution flags' which affect matching: if
  5011.    REG_NOTBOL is set, then ^ does not match at the beginning of the
  5012.    string; if REG_NOTEOL is set, then $ does not match at the end.
  5013.    
  5014.    We return 0 if we find a match and REG_NOMATCH if not.  */
  5015.  
  5016. int
  5017. regexec (preg, string, nmatch, pmatch, eflags)
  5018.     const regex_t *preg;
  5019.     const char *string; 
  5020.     size_t nmatch; 
  5021.     regmatch_t pmatch[]; 
  5022.     int eflags;
  5023. {
  5024.   int ret;
  5025.   struct re_registers regs;
  5026.   regex_t private_preg;
  5027.   int len = strlen (string);
  5028.   boolean want_reg_info = !preg->no_sub && nmatch > 0;
  5029.  
  5030.   private_preg = *preg;
  5031.   
  5032.   private_preg.not_bol = !!(eflags & REG_NOTBOL);
  5033.   private_preg.not_eol = !!(eflags & REG_NOTEOL);
  5034.   
  5035.   /* The user has told us exactly how many registers to return
  5036.      information about, via `nmatch'.  We have to pass that on to the
  5037.      matching routines.  */
  5038.   private_preg.regs_allocated = REGS_FIXED;
  5039.   
  5040.   if (want_reg_info)
  5041.     {
  5042.       regs.num_regs = nmatch;
  5043.       regs.start = TALLOC (nmatch, regoff_t);
  5044.       regs.end = TALLOC (nmatch, regoff_t);
  5045.       if (regs.start == NULL || regs.end == NULL)
  5046.         return (int) REG_NOMATCH;
  5047.     }
  5048.  
  5049.   /* Perform the searching operation.  */
  5050.   ret = re_search (&private_preg, string, len,
  5051.                    /* start: */ 0, /* range: */ len,
  5052.                    want_reg_info ? ®s : (struct re_registers *) 0);
  5053.   
  5054.   /* Copy the register information to the POSIX structure.  */
  5055.   if (want_reg_info)
  5056.     {
  5057.       if (ret >= 0)
  5058.         {
  5059.           unsigned r;
  5060.  
  5061.           for (r = 0; r < nmatch; r++)
  5062.             {
  5063.               pmatch[r].rm_so = regs.start[r];
  5064.               pmatch[r].rm_eo = regs.end[r];
  5065.             }
  5066.         }
  5067.  
  5068.       /* If we needed the temporary register info, free the space now.  */
  5069.       free (regs.start);
  5070.       free (regs.end);
  5071.     }
  5072.  
  5073.   /* We want zero return to mean success, unlike `re_search'.  */
  5074.   return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
  5075. }
  5076.  
  5077.  
  5078. /* Returns a message corresponding to an error code, ERRCODE, returned
  5079.    from either regcomp or regexec.   We don't use PREG here.  */
  5080.  
  5081. size_t
  5082. regerror (errcode, preg, errbuf, errbuf_size)
  5083.     int errcode;
  5084.     const regex_t *preg;
  5085.     char *errbuf;
  5086.     size_t errbuf_size;
  5087. {
  5088.   const char *msg;
  5089.   size_t msg_size;
  5090.  
  5091.   if (errcode < 0
  5092.       || errcode >= (sizeof (re_error_msg) / sizeof (re_error_msg[0])))
  5093.     /* Only error codes returned by the rest of the code should be passed 
  5094.        to this routine.  If we are given anything else, or if other regex
  5095.        code generates an invalid error code, then the program has a bug.
  5096.        Dump core so we can fix it.  */
  5097.     abort ();
  5098.  
  5099.   msg = re_error_msg[errcode];
  5100.  
  5101.   /* POSIX doesn't require that we do anything in this case, but why
  5102.      not be nice.  */
  5103.   if (! msg)
  5104.     msg = "Success";
  5105.  
  5106.   msg_size = strlen (msg) + 1; /* Includes the null.  */
  5107.   
  5108.   if (errbuf_size != 0)
  5109.     {
  5110.       if (msg_size > errbuf_size)
  5111.         {
  5112.           strncpy (errbuf, msg, errbuf_size - 1);
  5113.           errbuf[errbuf_size - 1] = 0;
  5114.         }
  5115.       else
  5116.         strcpy (errbuf, msg);
  5117.     }
  5118.  
  5119.   return msg_size;
  5120. }
  5121.  
  5122.  
  5123. /* Free dynamically allocated space used by PREG.  */
  5124.  
  5125. void
  5126. regfree (preg)
  5127.     regex_t *preg;
  5128. {
  5129.   if (preg->buffer != NULL)
  5130.     free (preg->buffer);
  5131.   preg->buffer = NULL;
  5132.   
  5133.   preg->allocated = 0;
  5134.   preg->used = 0;
  5135.  
  5136.   if (preg->fastmap != NULL)
  5137.     free (preg->fastmap);
  5138.   preg->fastmap = NULL;
  5139.   preg->fastmap_accurate = 0;
  5140.  
  5141.   if (preg->translate != NULL)
  5142.     free (preg->translate);
  5143.   preg->translate = NULL;
  5144. }
  5145.  
  5146. #endif /* not emacs  */
  5147.  
  5148. /*
  5149. Local variables:
  5150. make-backup-files: t
  5151. version-control: t
  5152. trim-versions-without-asking: nil
  5153. End:
  5154. */
  5155.